Note that there are some explanatory texts on larger screens.

plurals
  1. POPerforming network and other time consuming tasks in background
    primarykey
    data
    text
    <p>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.</p> <p>I think there are 2 ways to do this:</p> <ol> <li><p>Perform asynchronous loading using <code>NSURLConnection</code> and in the <code>didFinishLoading</code> method use <strong>GCD</strong> (say) to do the processing in background.</p></li> <li><p>Use <strong>GCD's</strong> <code>dispatch_async</code> (say) to start work in background and use <code>NSURLConnection's</code> <code>sendSynchronousRequest:returningResponse:error</code> to download the data synchronously, Do the processing of the data, And call <code>UI</code> updates on the main thread.</p></li> </ol> <p>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:</p> <p><strong>main (start) -> background (download) -> main (<code>NSURLConnectionDelegate</code> method) -> background (data processing) -> main (<code>UI</code> update</strong>)</p> <p>We would have:</p> <p><strong>main (start) -> background (download) -> background (data processing) -> main (UI update) which seems to be cleaner to me.</strong></p> <p>I found 2 similar questions: <a href="https://stackoverflow.com/questions/6591416/good-pattern-for-internet-requests-with-grand-central-dispatch">Good pattern for Internet requests with Grand Central Dispatch?</a> And<br> <a href="https://stackoverflow.com/questions/5037545/nsurlconnection-and-grand-central-dispatch">NSURLConnection and grand central dispatch</a></p> <p>And the answers to both seem to suggest using something conceptually similar to method 1.</p> <p>Is there no proper way to achieve what's described in method 2?</p> <p>Thanks in advance!</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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