magicalpanda / MagicalRecord

Super Awesome Easy Fetching for Core Data!
Other
10.8k stars 1.79k forks source link

Concurrency issue: saveWithBlock:completion crashes when setting entity attribute. #1077

Closed danielwmsun closed 9 years ago

danielwmsun commented 9 years ago

I'm using saveWithBlock:completion to create an instance of my User Entity and set some of its attributes in the completion block of an API call. If I do this:

 NSDictionary *userDict = (NSDictionary *)responseObject;
 [MagicalRecord saveWithBlock:^(NSManagedObjectContext *localContext) {
            SLUser *user = [SLUser MR_createEntityInContext:localContext];
            user.firstName = userDict[@"first_name"];
        } completion:^(BOOL contextDidSave, NSError *error) {
            if (error) {
                 NSLog(@"error: %@", error.localizedDescription);
            }
        }];

If I do this, the app breaks and I get a __pthread_kill signal.

I am using AFNetworking, which calls it's completion blocks on the main thread, so I don't think that I am in a separate background.

I'm not sure why this is happening, especially since I have almost identical code in various other places, where I create an entity within the saveWithBlock:completion method, and I set attributes directly afterwards.