NSOperationQueue not working in iOS5

I have a project that loads images in the background using NSOperationQueue . It has worked so far on devices with iOS 4.3. However, if I create an application with basic sdk 4.3 or 5 and run the application on an iOS5 device, the application will fail. When the application is launched, it adds NSOperation objects to the queue for loading images. If between them I click the back button, I cancel NSOperation and it crashes and displays the following trace on the 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 starting the queue it is in" If I comment on the call to the cancel method, the application will not work. Are there any updates for NSOperation changes for NSOperation ?

+7
source share
1 answer

I had the same problem when creating against iOS 5. I ended up creating a flag named operationStarted , which was NO by default, and I switched to YES when the start method was called. Then, in my finish method (where I generate KVO notifications), I checked the flag value before starting the notifications.

The flag definition looks like this:

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

start method:

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

My finish method, which is called when the operation is completed or canceled:

 - (void)finish { if (![self isOperationStarted]) return; [self willChangeValueForKey:@"isExecuting"]; executing = NO; [self didChangeValueForKey:@"isExecuting"]; [self willChangeValueForKey:@"isFinished"]; finished = YES; [self didChangeValueForKey:@"isFinished"]; } 

It ended up resolving the issue for me. Hope this helps someone else.

+12
source

All Articles