check when two NSOperationQueue have finished to c

2019-08-23 01:13发布

问题:

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?

回答1:

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