Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Instead of processing the JSON array and then looping and fetching all those images on the main thread, why not do it on a background thread. In the background, run the loop, and at the end, call another method on the main to signal the completed task. </p> <pre class="lang-c prettyprint-override"><code>//star the loop to downloading the ten images ... in the background dispatch_queue_t currentQueue = dispatch_get_current_queue(); dispatch_retain(currentQueue); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0, ^(void) { for (int i=0; i&lt;10; i++) { //find image[i] url from json NSString *fullImage_URL = json[i][@"url"]; //download image synchronously (this blocks the UI!!) NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fullImage_URL]]; //insert image in the array arrayImages[i] = [UIImage imageWithData:data]; } // Return to main queue dispatch_async(currentQueue, ^{ // process arrayImages now }); dispatch_release(currentQueue); }); </code></pre> <p>To read more about the dispatch queues, check this out:</p> <p><a href="https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1" rel="nofollow">https://developer.apple.com/library/ios/documentation/General/Conceptual/ConcurrencyProgrammingGuide/OperationQueues/OperationQueues.html#//apple_ref/doc/uid/TP40008091-CH102-SW1</a></p> <p>There are many other ways to do this. Personally, I'm a fan of using NSNotificationCenter myself, and the OBSERVER pattern.</p> <p><a href="https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html" rel="nofollow">https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSNotificationCenter_Class/Reference/Reference.html</a></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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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