I have a very strange crash, and no idea what am I doing wrong. This is the code.
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
[MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
Contact *contact = [Contact MR_findFirstByAttribute:kCoreData_Contact_ID withValue:strContactID inContext:localContext];
strFilePath = [Util buildFilePathForImageWithID:contact.c_id withKey:@"ContactPicture"];
contact.c_photoFilePath = strFilePath;
} completion:^(BOOL success, NSError *error) {
}];
Contact *contact = [Contact getContactWithID:strContactID];
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:contact.c_photoURL]];
[data writeToFile:strFilePath atomically:YES];
UIImage *image = [UIImage imageWithData:data];
UIImage *imgToShow = nil;
if (image)
{
imgToShow = [SquareAndMask maskImage_72:image];
}
else
{
imgToShow = [UIImage imageNamed:@"add_rv_photo"];
}
dispatch_async(dispatch_get_main_queue(), ^{
//> Image downloaded, load it
[self.imgViewPhoto setImage:imgToShow];
self.imgViewStatusLevel.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@_btn",contact.c_status]];
});
});
Right now there are some confusions in my head regarding multithreading with MR, that's why the above code might have issues. However, I cannot understand why is it crashing.
Note: I am testing on simulator, and on iPad mini retina, and for me, there is no crash. My client is testing on iPad 3,4 and he has the crash everything when this part of code is run.
What I am doing in this piece of code: I have a Contact object, which saves the url and filePath of a photo. So first time, there is no image saved locally, I run this code, download the image, and save it locally, and I also save a c_photoFilePath, so next time I know where to take that image from.
I symbolicated some crash logs, and this is what I got:
Also, once I got this in simulator, bot not sure if it's related to this crash:
CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[__NSCFSet addObject:]: attempt to insert nil with userInfo (null)
I accept any curse for the above code :) just please help me.