NSOperationQueue not working in IOS5

六眼飞鱼酱① 提交于 2019-12-21 02:27:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/7791789/nsoperationqueue-not-working-in-ios5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!