I create an NSOperation every time my app launches or resigns active. I need to queue them with dependencies such that two never execute at the same time, but one after another.
Is it safe to do this?
- Hold a strong reference to the NSOperation object in the App Delegate.
- When the app res开发者_开发问答igns active, simply check if hat property is not nil.
- If it is not nil, check if the current NSOperation
-isFinished
. - If it's finished, just add the new one to the queue.
- If it's not finished yet, create the new one and set a dependency on the running one, then add it to the queue.
I'm concerned a bit with multithreading issues here. The documentation of the -isFinished or -addDependency: methods doesn't say they should not be called from the main thread. So I guess it is ok to do that.
Edit: The NSOperation performs some file system operations in the background.
If you want to ensure they are not called at the same time, set the maximumConcurrentOperationCount: on your NSOperationQueue to 1.
- (void)setMaxConcurrentOperationCount:(NSInteger)count
This assumes you are putting both of your NSOperations in the same queue.
In response to your other questions. I'm not sure what you are doing - but yes you can hold strong a reference to your NSOperation on the AppDelegate if you want, and you can check isFinished
精彩评论