Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 8 CoreData Issue 'NSInternalInconsistencyException', reason: 'recordChangeSnapshot:forObjectID:: global ID may not be temporary when recording'

I am having real difficulties resolving this issue when migrating my App from iOS7 to iOS8, the app works perfect on iOS7.

It crashes when I am trying to save

NSError *error = nil;
if (![managedObjectContext save:&error]) {  <---- crashes here
     NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
 }

I've seen only 2 similar issues on stackoverflow but none of the responses were relevant to my problem.

iOS 8 CoreData Issue: recordChangeSnapshot:forObjectID:: global ID may not be temporary when recording

iOS 8 Core Data Issue: global ID may not be temporary when recording

Its not easy to post all the code. I don't know where to begin debugging !, some help please. Thanks

like image 642
Wael Avatar asked Dec 25 '22 02:12

Wael


1 Answers

Is this call to [managedObjectContext save:&error] happening on a background thread? Is it usually called on the main thread?

The pattern recommended for concurrent programming with Core Data is thread confinement: each thread must have its own entirely private managed object context.

When working with your Core Data code, you can use isMainThread to determine if you're on a background thread and switch to the main thread prior to working with Core Data:

- (void)yourCoreDataTask {
    if (![[NSThread currentThread] isMainThread]) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self yourCoreDataTask];
        });
        return;
    }

    // Now on the Main Thread. Work with Core Data safely.
}
like image 199
pkamb Avatar answered Dec 28 '22 08:12

pkamb