Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 368
dariaa Avatar asked Mar 27 '12 09:03

dariaa


2 Answers

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.

like image 135
mattjgalloway Avatar answered Nov 15 '22 21:11

mattjgalloway


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
    }
});
like image 40
Ashley Mills Avatar answered Nov 15 '22 20:11

Ashley Mills