Note that there are some explanatory texts on larger screens.

plurals
  1. POImages Persistence and Lazy Loading Conflict With Dispatch_Async
    text
    copied!<p>I am working on a feed reader and i am doing it by parsing rss feeds using nsxmlparser. I also have thumbnail objects that i am taking from the CDATA block.</p> <pre><code>-(void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { 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]; }); } </code></pre> <p>I am persisting the images and other tableview objects using archiving and encode and decode methods. </p> <p>The issue is when i use the above code as it is, it does not persist images while other tableview objects like title and published date do persist. But when i use the above code without dispatch_async around it, then it starts persisting the images but locks the user interface.</p> <p>How can i persist the images without locking the user interface? </p> <p>Kindly do not answer with some library to store and cache images or Apple's lazy loading example. Thanks</p> <p><strong>UPDATE:</strong></p> <p>Based on the answer provided and after watching a lecture by Stanford in iTunes on Multithreading, i have implemented the above code as follow:</p> <pre><code> NSString *someString = [[NSString alloc] initWithData:CDATABlock encoding:NSUTF8StringEncoding]; NSString *storyImageURL = [self getFirstImageUrl:someString]; NSURL *tempURL = [NSURL URLWithString:storyImageURL]; dispatch_queue_t downloadQueue = dispatch_queue_create("image downloader", NULL); dispatch_async(downloadQueue, ^{ NSData *tempData = [NSData dataWithContentsOfURL:tempURL]; dispatch_async(dispatch_get_main_queue(), ^{ thumbnail = [UIImage imageWithData:tempData]; }); }); } </code></pre> <p>Logically, everything is correct now but still it didn't work. I am all blocked how to solve this issue. </p> <p><strong>UPDATE:</strong></p> <p>I am using the Model View Controller Store approach and my store and connection code are defined in separate files. Below is the code where i am using NSKeyedArchiver.</p> <pre><code>- (FeedChannel *) FeedFetcherWithCompletion:(void (^)(FeedChannel *, NSError *))block { NSURL *url = [NSURL URLWithString:@"http://techcrunch.com/feed/"]; NSURLRequest *req = [NSURLRequest requestWithURL:url]; FeedChannel *channel = [[FeedChannel alloc] init]; TheConnection *connection = [[TheConnection alloc] initWithRequest:req]; NSString *pathOfCache = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; pathOfCache = [pathOfCache stringByAppendingPathComponent:@"check.archive"]; FeedChannel *channelCache = [NSKeyedUnarchiver unarchiveObjectWithFile:pathOfCache]; if (!channelCachel) { channelCache = [[FeedChannel alloc] init]; } FeedChannel *channelCopy = [channelCache copy]; [connection setCompletionBlock:^(FeedChannel *obj, NSError *err) { if (!err) { [channelCopy addItemsFromChannel:obj]; [NSKeyedArchiver archiveRootObject:channelCopy toFile:pathOfCache]; } block(channelCopy, err); }]; [connection setXmlObject:channel]; [connection start]; return channelCache; } </code></pre> <p>and below are my encode and decode methods. </p> <pre><code>- (void)encodeWithCoder:(NSCoder *)aCoder { [aCoder encodeObject:title forKey:@"title"]; [aCoder encodeObject:link forKey:@"link"]; [aCoder encodeObject:creator forKey:@"creator"]; [aCoder encodeObject:pubDate forKey:@"pubDate"]; [aCoder encodeObject:thumbnail forKey:@"thumbnail"]; //[aCoder encodeObject:UIImagePNGRepresentation(thumbnail) forKey:@"thumbnail"]; [aCoder encodeObject:publicationDate forKey:@"publicationDate"]; } - (id)initWithCoder:(NSCoder *)aDecoder { self = [super init]; if (self) { [self setTitle:[aDecoder decodeObjectForKey:@"title"]]; [self setLink:[aDecoder decodeObjectForKey:@"link"]]; [self setCreator:[aDecoder decodeObjectForKey:@"creator"]]; [self setPubDate:[aDecoder decodeObjectForKey:@"pubDate"]]; [self setThumbnail:[aDecoder decodeObjectForKey:@"thumbnail"]]; //[self setThumbnail:[UIImage imageWithData:[aDecoder decodeObjectForKey:@"thumbnail"]]]; [self setPublicationDate:[aDecoder decodeObjectForKey:@"publicationDate"]]; } return self; } </code></pre> <p>Based on the answer, i don't know whether i have to use some extra code to persist images. because other objects like title, creator etc. are persisting accurately.</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