Note that there are some explanatory texts on larger screens.

plurals
  1. POClarifications needed for concurrent operations, NSOperationQueue and async APIs
    primarykey
    data
    text
    <p>This is a two part question. Hope someone could reply with a complete answer.</p> <p><code>NSOperation</code>s are powerful objects. They can be of two different types: non-concurrent or concurrent.</p> <p>The first type runs synchronously. You can take advantage of a non-concurrent operations by adding them into a <code>NSOperationQueue</code>. The latter creates a thread(s) for you. The result consists in running that operation in a concurrent manner. The only caveat regards the lifecycle of such an operation. When its <code>main</code> method finishes, then it is removed form the queue. This is can be a problem when you deal with async APIs.</p> <p>Now, what about concurrent operations? From Apple doc</p> <blockquote> <p>If you want to implement a concurrent operation—that is, one that runs asynchronously with respect to the calling thread—you must write additional code to start the operation asynchronously. For example, you might spawn a separate thread, call an asynchronous system function, or do anything else to ensure that the start method starts the task and returns immediately and, in all likelihood, before the task is finished.</p> </blockquote> <p>This is quite almost clear to me. They run asynchronously. But you must take the appropriate actions to ensure that they do. </p> <p>What it is not clear to me is the following. Doc says:</p> <blockquote> <p>Note: In OS X v10.6, operation queues ignore the value returned by isConcurrent and always call the start method of your operation from a separate thread.</p> </blockquote> <p>What it really means? <strong>What happens if I add a concurrent operation in a <code>NSOperationQueue</code>?</strong></p> <p>Then, in this post <a href="http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent_operations/" rel="nofollow">Concurrent Operations</a>, concurrent operations are used to download some HTTP content by means of <code>NSURLConnection</code> (in its async form). Operations are concurrent and included in a specific queue.</p> <pre><code>UrlDownloaderOperation * operation = [UrlDownloaderOperation urlDownloaderWithUrlString:url]; [_queue addOperation:operation]; </code></pre> <p>Since <code>NSURLConnection</code> requires a loop to run, the author shunt the <code>start</code> method in the main thread (so I suppose adding the operation to the queue it has spawn a different one). In this manner, the main run loop can invoke the delegate included in the operation.</p> <pre><code>- (void)start { if (![NSThread isMainThread]) { [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO]; return; } [self willChangeValueForKey:@"isExecuting"]; _isExecuting = YES; [self didChangeValueForKey:@"isExecuting"]; NSURLRequest * request = [NSURLRequest requestWithURL:_url]; _connection = [[NSURLConnection alloc] initWithRequest:request delegate:self]; if (_connection == nil) [self finish]; } - (BOOL)isConcurrent { return YES; } // delegate method here... </code></pre> <p>My question is the following. <strong>Is this thread safe?</strong> The run loop listens for sources but invoked methods are called in a background thread. Am I wrong?</p> <p><strong>Edit</strong></p> <p>I've completed some tests on my own based on the code provided by Dave Dribin (see <a href="http://www.dribin.org/dave/blog/archives/2009/05/05/concurrent_operations/" rel="nofollow">1</a>). I've noticed, as you wrote, that callbacks of <code>NSURLConnection</code> are called in the <strong>main thread</strong>.</p> <p>Ok, but now I'm still very confusing. I'll try to explain my doubts.</p> <p>Why including within a <strong>concurrent</strong> operation an async pattern where its callback are called in the main thread? Shunting the <code>start</code> method to the main thread it allows to execute callbacks in the main thread, and what about queues and operations? Where do I take advantage of threading mechanisms provided by GCD?</p> <p>Hope this is clear. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload