0

in my app i have some NSOperation that update some core data element from a online database, sometime the update require some minute, and when the screen of iPhone lock, the app enter in the background mode, and this update is stopped, so i have to reopen the app to continue the update, so i have asked this some minute ago on SO, i have received this correct answer:

- (void)someMethodToKeepRunningInBackground {
UIBackgroundTaskIdentifier taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
    // Uh-oh - we took too long. Stop task.
}];

// Perform task here
CheckForUpdate *checkUpdate = [[CheckForUpdate alloc] init];
[sectionCheckUpdateQueue addOperation:checkUpdate];       

if (taskId != UIBackgroundTaskInvalid) {
    [[UIApplication sharedApplication] endBackgroundTask:taskId];
}
}

the problem is that i have the more NSOperationQueue that run to perform the update, the sectionCheckUpdateQueue that i write above, that if found update schedule more NSOperation on other NSOperationQueue, so i can't call this:

endBackgroundTask:taskId

because i don't know when this NSOperationQueue are finished, so my question is, so how i can detect when this NSOperationQueue are finished so to call the endBacgkround:taskId?

Piero
  • 9,173
  • 18
  • 90
  • 160
  • You can use completion block of `NSOperations` to find this out right? Some kind of mechanism which will detect if all operations are completed based on this completion block call and once the total count is zero, you can do `endBackgroundTask`. – iDev Nov 27 '12 at 00:57
  • you can write me an example please? – Piero Nov 27 '12 at 00:58
  • Or else check with this, `operationCount` property of `NSOperationQueue`. https://developer.apple.com/library/mac/documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/occ/instm/NSOperationQueue/operationCount – iDev Nov 27 '12 at 01:07
  • I know that property...but where i have to use it?...Give me an example please... – Piero Nov 27 '12 at 01:26
  • i can't find any completion block for NSOperationQueue – Piero Nov 27 '12 at 09:14

1 Answers1

2

You could call waitUntilAllOperationsAreFinished on the NSOperationQueue to know when it is done with all operations.

https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/NSOperationQueue_class/Reference/Reference.html#//apple_ref/occ/instm/NSOperationQueue/waitUntilAllOperationsAreFinished

Abhishek Bedi
  • 5,205
  • 2
  • 36
  • 62
user1885297
  • 586
  • 2
  • 6