我使用NSOperation和NSOperationQueue(用于2D游戏)在后台加载图像。NSOperationQueue阻止无关的NSOperationQueue?
要了解如何NSOperations行为,我已经试过加入以下无关NSOperationQueue /的NSOperation(我称之为-startNewEndlessBackgroundTask方式在开始任何图像加载前):
static int stop = NO;
static int c = 1000;
-(void)takeTime {
stop = NO;
while (!stop) {
for (int i = 0; i < 10000; i++) {
c += 1;
}
c = c;
}
}
-(void)stopBackgroundTask {
stop = YES;
}
-(void)startNewEndlessBackgroundTask {
//[self performSelectorInBackground:@selector(takeTime) withObject:nil];
NSOperationQueue* queue = [[NSOperationQueue alloc] init];
[queue addOperationWithBlock:^{
[self takeTime];
}];
}
这完全阻断了我的其他NSOperationQueue的图像加载在iPhone4上。在iPhone4s上,2次调用startNewEndlessBackgroundTask后,它会阻止我的图像加载。两者都运行iOS 6.
我的应用程序的主循环未被阻止。
如果我改用performSelectorInBackground来调用takeTime,一切正常,没有阻塞,并且takeTime例程在后台也能正常工作。
为什么会发生这种情况,当两个NSOperationQueue完全分开分配并且没有依赖关系?对我来说,以这种简单的方式使用NSOperationQueue和使用performSelectorInBackground应该没有什么区别,但是我猜这是我误解的基本原理吗?