I'm developing a network based iOS app that downloads json data from the server and processes it. Both the downloading task and processing task can take a significant time to complete, So I don't want to perform either on the main thread.
I think there are 2 ways to do this:
Perform asynchronous loading using
NSURLConnection
and in thedidFinishLoading
method use GCD (say) to do the processing in background.Use GCD's
dispatch_async
(say) to start work in background and useNSURLConnection's
sendSynchronousRequest:returningResponse:error
to download the data synchronously, Do the processing of the data, And callUI
updates on the main thread.
I think the 2nd method would be easier to write and would produce cleaner code, Especially if one "download/process data" task involves multiple sequential service calls for data download. So rather than execution going like:
main (start) -> background (download) -> main (NSURLConnectionDelegate
method) -> background (data processing) -> main (UI
update)
We would have:
main (start) -> background (download) -> background (data processing) -> main (UI update) which seems to be cleaner to me.
I found 2 similar questions: Good pattern for Internet requests with Grand Central Dispatch?
And
NSURLConnection and grand central dispatch
And the answers to both seem to suggest using something conceptually similar to method 1.
Is there no proper way to achieve what's described in method 2?
Thanks in advance!