Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSOperationQueue is crashing in IOS

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?

like image 232
CKT Avatar asked Oct 17 '11 09:10

CKT


1 Answers

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.

like image 138
RJ Regenold Avatar answered Sep 24 '22 05:09

RJ Regenold