Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data - cannot store two separate entities

I have Core Data working correctly (loading, saving) with one entity called THSettings.

However, I want to create another unrelated Core Data entity called THDetails.

When I create another entity in the same xcdatamodeld file it gives me this error when running the app: "The model used to open the store is incompatible with the one used to create the store".

So I removed that entity and created another xcdatamodeld file for the new entity and now it gives this error: "* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'NSFetchRequest could not locate an NSEntityDescription for entity name 'THDetails''"

Can anyone please tell me why this does not work? I should be able to save multiple unrelated entities...

Here is the code to load THDetails:

-(NSMutableArray *)loadSavedNotes {
    THAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *moc = [appDelegate managedObjectContext];
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"THDetails"];
    NSMutableArray *detailsList = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];
    NSLog(@"%d", [detailsList count]);
    return detailsList;
}

Here is the code to load THSettings:

THAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
NSManagedObjectContext *moc = [appDelegate managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:@"THSettings"];
NSMutableArray *settingsList = [[moc executeFetchRequest:fetchRequest error:nil] mutableCopy];


// ... process it
like image 997
user2894272 Avatar asked Jun 28 '26 09:06

user2894272


1 Answers

You need to define a model that defines all your entities. If you add an additional entity to a model, you have two options:

  • Just add the new entity to the existing model. In this case you have to remove the app from the Simulator or device before running the new code to start with a new fresh database that is compatible with the changed model. This is usually done during development, when there is no need to preserve data. OR:
  • Create a model version and add the "lightweight migration" options when the Core Data stack is initialized. In this case both the old and the new model are copied into the application bundle, and Core Data can (under some conditions) migrate the old database to a new database. This would be done if an update is made for an already distributed app, and you want that the data is preserved. "Lightweight migration" works for simple changes in the model, such as adding a new entity. See "Core Data Model Versioning and Data Migration Programming Guide" for more information.
like image 90
Martin R Avatar answered Jun 30 '26 22:06

Martin R



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!