I tried to build a table view with help of coredata and FetchedResultsController (info from coredata is take it with an API from a server), each cell from table has a image view which load images from net asynchronous with GCD (also I tried and with SDWebImage) in method
"tableView:tableView cellForRowAtIndexPath:indexPath", the problem appear when I make another request for more records (for example first time I have 50 records, and when I do a new request and save it in core data the images are no more correct associated with article or disappear on scrolling) I believe because the results from fetchedResultsController are sorted in function of time.
My code:
NewsFeed *singleFeed = [self.fetchedResultsController objectAtIndexPath:indexPath];
NLNewsFeedCell *cell = (NLNewsFeedCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"NewsFeedCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
cell.lblTextContain.numberOfLines = 0;
}
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^(void) {
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:singleFeed.urlPicture]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
cell.imgPicture.image = image;
[cell setNeedsLayout];
});
}
});
}
Any suggestion, how can solve this problem? Thanks and sorry for misspelling.