0

We have migrated our app from Xcode 6.4 to Xcode 7 and Swift 2. The app uses CoreData with 2 MOCs and was working well without any multithread violations warning. And now, compiling from Xcode 7, under devices or simulators running iOS 8.4 and 8.3 we get the famous "Multithreading_Violation_AllThatIsLeftToUsIsHonor". It works well on devices running any iOS 9 and iOS 8 before 8.3. And also works well with Xcode 6.4 and all iOS versions.

Xcode 6.4 = Works well on all iOS

Xcode 7 = Breaks on 8.3 and 8.4 only

We have checked our code and I think it's all OK.

The line that fires the error is result = [context executeFetchRequest:request error:&error];

- (void)getActiveUserWithCompletion:(void (^)(User *user))completion {

NSManagedObjectContext *context = [[CoreDataManager singleton] getInsertContext];
NSManagedObjectContext *mainContext = [[CoreDataManager singleton]getContext];

__block NSError * error = nil;
__block NSArray *result = [NSArray array];

[context performBlock:^{
    NSFetchRequest *request= [[NSFetchRequest alloc] init];
    [request setEntity: [NSEntityDescription entityForName:K_COREDATA_USER inManagedObjectContext:context]];
    [request setPredicate:[NSPredicate existToken]];
    [request setFetchLimit:1];

    result = [context executeFetchRequest:request error:&error];
    if ( result.count == 1 ) {
        User *user = result.firstObject;
        NSManagedObjectID *moid = [user objectID];
        [mainContext performBlock:^{
            User *activeUser = (User *)[mainContext existingObjectWithID:moid error:&error];
            completion(activeUser);
        }];

    }else if ( result.count > 1 ) {
        completion(nil);
    }else {
        completion(nil);
    }
}];
}

Predicate code:

+ (NSPredicate *)existToken{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"sessionToken != nil"];
return predicate;
}

A very similar error is on Apple dev forums without any working solution: Apple Developer forum

Any advice or solution is welcome.

Brian
  • 14,610
  • 7
  • 35
  • 43
Christian
  • 382
  • 2
  • 11

0 Answers0