Question

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:

Crash1

Crash2

Crash3

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.

Was it helpful?

Solution

First rule of thumb advice: if you have randomly crashing code, it's likely you are sung threads or concurrency incorrectly.

Second, what you are doing here is dispatching a dispatch in the background. The saveWithBlock method returns immediately. The code after it expects a synchronous result, and is likely crashing because your data isn't ready yet. You are also not crossing thread boundaries correctly with core data. I suggest looking at the MR_inContext: method in the MagicalRecord library. As for a fix, I suggest not wrapping a saveWithBlock call with the dispatch. Also, put all the code you want to be run after the data is ready in the completion block. That is what it's for.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top