magicalpanda / MagicalRecord

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

New contexts with MR 3 on background threads trigger concurrency violations #1131

Open twobitlabs opened 8 years ago

twobitlabs commented 8 years ago

I'm upgrading a project to MR 3 (which is looking great by the way!) and have turned on concurrency debugging (-com.apple.CoreData.ConcurrencyDebug 1) to make sure we're I'm doing everything right. Whenever we try and create contexts on a background thread it's triggering the multithreading violation exception:

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSManagedObjectContext *context = [NSManagedObjectContext MR_privateQueueContext]; // this line triggers the exception, same issue if we use MR_context
    });

The following MR 3 code that's trying to modify the NSManagedObjectContext's userInfo is triggering the Multithreading_Violation_AllThatIsLeftToUsIsHonor error:

- (void) MR_setWorkingName:(NSString *)workingName;
{
    [[self userInfo] setObject:workingName forKey:kMagicalRecordNSManagedObjectContextWorkingName];
}

Are we doing something wrong?

twobitlabs commented 8 years ago

I should add that if I put the call to MR_setWorkingName into a performAndWaitBlock it works:

+ (NSManagedObjectContext *) MR_privateQueueContext;
{
    NSManagedObjectContext *context = [[self alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
    [context performBlockAndWait:^{
        [context MR_setWorkingName:@"Private Queue"];
    }];
    return context;
}
casademora commented 8 years ago

Yep you're likely correct. We need to do a threading audit with the core data thread debug flag on before we are finally able to merge 3.0 back to master

Sent from my iPhone

On Nov 13, 2015, at 9:20 AM, Two Bit Labs notifications@github.com wrote:

I should add that if I put the call to MR_setWorkingName into a performAndWaitBlock it works:

  • (NSManagedObjectContext ) MR_privateQueueContext; { NSManagedObjectContext context = [[self alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType]; [context performBlockAndWait:^{ [context MR_setWorkingName:@"Private Queue"]; }]; return context; } — Reply to this email directly or view it on GitHub.