NSOperationQueue 在 IOS 中崩溃 [英] NSOperationQueue is crashing in IOS
问题描述
我有一个使用 NSOperationQueue
在后台下载图像的项目.到目前为止,它一直在 IOS 4.3 的设备上运行.但是,如果我使用基础 sdk 4.3 或 5 构建应用程序并在使用 IOS5 的设备上运行应用程序,应用程序就会崩溃.当应用程序启动时,它会将 NSOperation
对象添加到下载图像的队列中.如果在这之间我按下后退按钮,我会取消 NSOperation
并且它崩溃并在控制台上显示以下跟踪:
并打印ResourceLoadOperation isFinished = YES 而不被它所在的队列启动"如果我评论取消方法调用,应用程序不会崩溃.IOS5 的 NSOperation
更改是否有任何更新?
我在针对 iOS 5 构建时遇到了同样的问题.我最终创建了一个名为 operationStarted
的标志,它是 NO
默认情况下,当调用 start
方法时,我切换到 YES
.然后在我的 finish
方法(我在其中生成 KVO 通知)中,我在触发通知之前检查了标志的值.
标志定义如下所示:
@property (nonatomic, assign, getter=isOperationStarted) BOOL operationStarted;
start
方法:
- (void)start {[self setOperationStarted:YES];...}
我的finish
方法,在操作完成或取消时调用:
- (void)finish {if (![self isOperationStarted]) 返回;[self willChangeValueForKey:@"isExecuting"];执行=否;[self didChangeValueForKey:@"isExecuting"];[self willChangeValueForKey:@"isFinished"];完成 = 是;[self didChangeValueForKey:@"isFinished"];}
这最终为我解决了问题.希望能帮到别人.
I have a project which downloads images in background using NSOperationQueue
. It was working until now on devices with IOS 4.3. However if I build the app with base sdk 4.3 or with 5 and run the app on device with IOS5, the app crashes. When app is launched, it adds NSOperation
objects into queue for downloading the images. If in between I press back button, I cancel the NSOperation
and it crashes and displays following trace on console:
#0 0x004727b7 in ____NSOQSchedule_block_invoke_0 () #1 0x026a5618 in _dispatch_call_block_and_release () #2 0x026a7a10 in _dispatch_worker_thread2 () #3 0x974bb781 in _pthread_wqthread () #4 0x974bb5c6 in start_wqthread ()
and prints "ResourceLoadOperation isFinished = YES without being started by the queue it is in"
If I comment the cancel method call, app doesnot crash.
Is there any updates on the NSOperation
changes for IOS5?
I had this same problem when building against iOS 5. I ended up creating a flag named operationStarted
that was NO
by default and I toggled to YES
when the start
method was called. Then in my finish
method (where I generate the KVO notifications), I checked the value of the flag before firing the notifications.
The flag definition looks like this:
@property (nonatomic, assign, getter=isOperationStarted) BOOL operationStarted;
The start
method:
- (void)start {
[self setOperationStarted:YES];
...
}
My finish
method which is called when the operation is complete or cancelled:
- (void)finish {
if (![self isOperationStarted]) return;
[self willChangeValueForKey:@"isExecuting"];
executing = NO;
[self didChangeValueForKey:@"isExecuting"];
[self willChangeValueForKey:@"isFinished"];
finished = YES;
[self didChangeValueForKey:@"isFinished"];
}
That ended up resolving the issue for me. Hope it helps someone else.
这篇关于NSOperationQueue 在 IOS 中崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!