Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>After reading your comment, I tend to think that the issue has to do with how you persist the images relative to the dispatch_async block. I will try to explain, but this is just a guess, since I don't know where you persist the image:</p> <ol> <li><p>at some point your <code>parser:foundCDATA:</code> is executed and you retrieve asynchronously your image;</p></li> <li><p>the image will not be there for some time (until the sync block completes), so the <code>thumbnail</code> ivar/property will not have the right value for some time after completing <code>parser:foundCDATA:</code>;</p></li> <li><p>now, if you persist <code>thumbnail</code> before the async block completes, you will persist the wrong (nil?) value;</p></li> <li><p>the fact that if you do not execute the fetching of the image asynchronously, then everything works, makes me think that you are persisting the image <em>too early</em> (in the above sense).</p></li> </ol> <p>The fix to this would be persisting the image only after the async block completes. This could mean something like:</p> <pre><code>dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding]; NSString *storyImageURL = [self getFirstImageUrl:someString]; NSURL *tempURL = [NSURL URLWithString:storyImageURL]; NSData *tempData = [NSData dataWithContentsOfURL:tempURL]; thumbnail = [UIImage imageWithData:tempData]; &lt;code here to make the thumbnail be persisted&gt; }); </code></pre> <p><code>&lt;code here to make the thumbnail be persisted&gt;</code> could be a method call, a notification post, whatever fits your current design. You might also need to use the trick of dispatching the call to the main thread if your persistence code is not thread safe:</p> <pre><code> dispatch_async(dispatch_get_main_queue(), ^{ &lt;persist thumbnail&gt;; } </code></pre> <p>Hope it helps.</p> <p>EDIT:</p> <p>After our chat, the above analysis is confirmed. In fact, you are not simply downloading one image: you are downloading a bunch of images, all belonging to the same "channel". This means that the persist operation, which is applied to the whole channel, shall be executed only after all of those images have been downloaded.</p> <p>I have thus forked your gist and added some logics enabling the use of GCD dispatch_group to keep track of all the download operations. You can find it <a href="https://gist.github.com/4579906" rel="nofollow">here</a>.</p>
 

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