I'm trying to perform a 2 step process:
, which is very similar to how facebook app works when you "Add Photo"
I want to start the upload of photos first in the background using the default NSOperationQueueDefaultMaxConcurrentOperationCount
. These operations will return some info that I need to send together with the 2nd post.
However, due to the nature of the app and because of concurrency, how can I structure it in the way that I post the content only if the following conditions are met:
A few scenarios can happen:
I think I should create a photo upload NSOperationQueue
and check for operationCount
. If it's == 0, the content should be posted immediately when the user taps on the UIButton
.
However, how do I wait if the photo upload is not complete? Should I be using waitUntilAllOperationsAreFinished
in the following way in pseudocode:
// User taps on UIButton to post his content and this is in a ViewController
- void postMyContent:
{
[self.photoUploadQ.waitUntilAllOperationsAreFinished]
// Post my content now
}
Is this correct? I can't add the 2nd Task to the photoUploadQ
since it has the default concurrency.
I'm concern of deadlock / blocking because of the documentation:
When called, this method blocks the current thread and waits for the receiver’s current and queued operations to finish executing. While the current thread is blocked, the receiver continues to launch already queued operations and monitor those that are executing. During this time, the current thread cannot add operations to the queue, but other threads may. Once all of the pending operations are finished, this method returns.
If there are no operations in the queue, this method returns immediately.
Thanks!
A common operation queue pattern is to create an operation (in your case, the "post content" operation) which is dependent upon the completion of a series of other operations (in your case, the "image upload" operations) using the NSOperation
method, addDependency
. This way, although the queue may be concurrent, this "post content" operation will not commence until the other "image upload" operations are done.
You may also want to constrain your operation queue to something less than NSOperationQueueDefaultMaxConcurrentOperationCount
because NSURLConnection
generally only permits 5 or so concurrent operations anyway (and if you do more than that, some of them may eventually timeout).