NSOperationQueue 在 IOS 中崩溃

发布于 2024-12-10 11:05:59 字数 597 浏览 0 评论 0原文

我有一个使用 NSOperationQueue 在后台下载图像的项目。到目前为止,它可以在 iOS 4.3 的设备上运行。但是,如果我使用基础 sdk 4.3 或 5 构建应用程序并在使用 IOS5 的设备上运行该应用程序,则该应用程序会崩溃。当应用程序启动时,它会将 NSOperation 对象添加到队列中以下载图像。如果在中间按下后退按钮,我会取消 NSOperation,它会崩溃并在控制台上显示以下跟踪:

#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 ()

并打印“ResourceLoadOperation isFinished = YES,而不会由它所在的队列启动” 如果我评论取消方法调用,应用程序不会崩溃。 IOS5 的 NSOperation 更改有任何更新吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女七分熟 2024-12-17 11:05:59

我在针对 iOS 5 构建时遇到了同样的问题。我最终创建了一个名为 operationStarted 的标志,默认情况下为 NO,然后我切换为 YES当调用 start 方法时。然后,在我的 finish 方法(生成 KVO 通知的地方)中,我在触发通知之前检查了标志的值。

标志定义如下所示:

@property (nonatomic, assign, getter=isOperationStarted) BOOL operationStarted;

start 方法:

- (void)start {
    [self setOperationStarted:YES];
    ...
}

当操作完成或取消时调用的我的 finish 方法:

- (void)finish {    
    if (![self isOperationStarted]) return;

    [self willChangeValueForKey:@"isExecuting"];
    executing = NO;
    [self didChangeValueForKey:@"isExecuting"];

    [self willChangeValueForKey:@"isFinished"];
    finished = YES;
    [self didChangeValueForKey:@"isFinished"];
}

最终为我解决了问题。希望它对其他人有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文