dariaa
dariaa

Reputation: 6385

issue with dispatch_async and async request

So the first question is how does dispatch_async determines which thread to use? just picks it randomly? I need to do some parsing and core data stuff, so I don't want to block UI thread and use dispatch_async, but after that I send a NSURLRequest to fetch some more data and the callback is never invoked (probably because the thread is already dead).

So what's a good way of making it? And I can not use

sendAsynchronousRequest:queue:completionHandler: 

because the deployment OS is 4. for now I just send request inside

dispatch_async(dispatch_get_main_queue(), ^{
});

which is inside dispatch_async(myQueue) block which does all the parsing and saving to core data. But it does not seem right to me, I mean there should be a way to use dispatch_async and tell it not kill the tread, right? Because using sync requests is not an option.

Upvotes: 5

Views: 6823

Answers (3)

Pascalius
Pascalius

Reputation: 14659

You are right, the problem is that the thread is dead, because the NSURLConnection won't keep it alive for you during its lifetime. The connection will be discarded when it returns. You can keep the thread alive explicitly with some dark magic ;) and release the thread when you are done. In this case by setting _isFinishedLoading to true on success or on error.

So in this case it is not true to not care about threads when using blocks, because NSURLConnection is not really compatible. Blocks however are extremely useful, since the framework chooses the best thread for you depending on the underlying hardware and available ressources.

NSURLConnection* connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
[connection start];
NSRunLoop *runloop = [NSRunLoop currentRunLoop];
_isFinishedLoading = NO;
while (!_isFinishedLoading)
{
    [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.1]];
}

Upvotes: 0

mattjgalloway
mattjgalloway

Reputation: 34902

So the first question is how does dispatch_async determines which thread to use?

You should not be thinking of threads when using GCD. That's all handled behind the scenes for you. You think in queues which is what you pass to dispatch_async along with the block to execute. A GCD queue does sort of map to a thread, but you don't need to worry about what is going on behind the scenes - GCD will handle all that for you.

As for your code which I assume looks similar to this:

dispatch_async(myQueue, ^{
    // Do some stuff off the main queue

    dispatch_async(dispatch_get_main_queue(), ^{ 
        // Do something with the UI
    });
});

There is nothing wrong with that at all. You do not need to worry that the thread that the block you sent to myQueue runs on, might be terminated. The queue will stay around until the block as finished running. The fact that you dispatch onto the main queue within the original dispatch is fine - the work on the main queue will happily run perfectly fine.

I think you're also asking why when using NSURLRequest within an async dispatch you are seeing it not ever invoke the callback. That will be because it's tightly coupled to the current run loop and if you're currently on a background thread (which your myQueue will be running on) then that run loop won't be run again until another block is put on the queue. So your callback never runs.

I encourage you to go and read more about GCD and what async vs sync actually means as I feel that you are possibly not fully understanding it yet. I answered a similar question about that here.

Upvotes: 12

Ashley Mills
Ashley Mills

Reputation: 53082

When you use sendAsynchronousRequest:... within a block, the block will terminate before the asynchronous request callback is fired.

As you're already in a background thread, how about using a synchronous method within that thread to get your data - +[NSURLRequest sendSynchronousRequest:returningResponse:error:] , or even simpler, +[NSData dataWithContentsOfURL:].

You can call these within the block running on a background thread, and the block won't progress until the call is complete. You can then update your UI by calling back to the main queue:

dispatch_async(yourQueue, ^{
    // Do some stuff

    NSData * fetchedData = [NSData dataWithContentsOfURL: someURL];

    // Do some more stuff with the data

    dispatch_async(dispatch_get_main_queue(), ^{
       // Do some stuff on the main queue
    }
});

Upvotes: 3

Related Questions