Tuesday, 10 September 2013

concurrent NSOperationQueue uploads followed by a single task

concurrent NSOperationQueue uploads followed by a single task

I'm trying to perform a 2 step process:
Concurrent upload of photos
User input then post content
, 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:
All photos have been uploaded
User action to post his content
A few scenarios can happen:
No photos to post, user post content, the content should be posted
immediately
Photo uploads are complete, user post content, content should be posted
immediately
Photo upload is not complete, user post content, need to wait for all
photos to upload finish before posting content.
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!

No comments:

Post a Comment