I have a series of profile pictures stored in Core Data as Binary Data
(with the Allows External Storage option enabled, so don't jump on me for storing images in Core Data :)
Each image is being displayed in a UITableViewCell
. At the moment there is a slight delay when the user taps to display the table view, presumably because it is loading them from Core Data which has a sufficient enough performance implication to lock-up the UI when loading them on the main thread.
I would like to put the image loading onto a separate background thread, so that the table view appears immediately and the images show when they have been loaded from Core Data.
I have tried the solution in this post with the following code in the -cellForRowAtIndexPath:
method:
// Create the cell
MyCell *cell = [tableView dequeueReusableCellWithReuseIdentifier:@"Cell" forIndexPath:indexPath];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
// Get a reference to the object for this cell (an array obtained from Core Data previously in the code)
MyObject *theObject = [self.objectArray objectAtIndex:indexPath.item];
// Load the photo from Core Data relationship (ProfilePhoto entity)
UIImage *profilePhoto = [UIImage imageWithData:theObject.profilePhoto.photo];
// Set the name
cell.nameLabel.text = theObject.name;
dispatch_sync(dispatch_get_main_queue(), ^(void) {
// Set the photo
cell.photoImageView.image = profilePhoto;
});
});
// Return the cell
return cell;
However, the app crashes with the following error (repeated for each row in the table view):
CoreData: error: exception during fetchRowForObjectID: statement is still active with userInfo of (null)
Any help on understanding and resolving this issue would be appreciated.