Вопрос

I'm really sorry for such a simple question but I tried several ways and cannot find a solution.

I just want to add a timestamp attribute in the ingredients entity. Whenever I change the recipes.xcdatamodel I get this:

error NSError domain: @"NSCocoaErrorDomain" - code: 134100 0x000000010c162890

I already tried model versioning and:

options:@{NSMigratePersistentStoresAutomaticallyOption:@YES,
NSInferMappingModelAutomaticallyOption:@YES}

The code is original as provided by Apple!

Это было полезно?

Решение

Select your .xcdatamodeld file in the Project Navigator.

Under the menu option "Editor", select "Add Model Version".

A new copy of your data model file will be created.

In this new copy, let's call it version 2 for simplicity, you need to add an attribute to store the time.

In version 2 of the .xcdatamodeld file (and I note here - be careful to select the new file and not the old file) add an attribute, call it "timeStamp", set the Type to "Date", and save.

Now when you run the sample project, the migration options you have included in your "Core Data Stack" will migrate the stored information from the old .xcdatamodeld file to version 2 of that file.

Also refer to... Core Data; Cocoa error 134100

UPDATE

I loaded iPhoneCoreDataRecipes sample code, and then followed these steps...

  • added a new version of the data model (as described above),
  • in the new version of the data model, being VERY CAREFUL to make certain that it is the new model I am modifying, added an attribute "timeStamp" to the Entity "Ingredients",
  • to the Ingredient.m file, added @dynamic timeStamp,
  • to the Ingredient.h file, added @property (nonatomic, retain) NSDate *timeStamp;,
  • to the RecipesAppDelegate.m file, added the following lines of code...

    NSMutableDictionary *options = [NSMutableDictionary dictionary];
    [options setValue:[NSNumber numberWithBool:YES]
               forKey:NSMigratePersistentStoresAutomaticallyOption];
    [options setValue:[NSNumber numberWithBool:YES]
               forKey:NSInferMappingModelAutomaticallyOption];
    
  • changed this line of code to include my above noted options,

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
    

and executed a Build & Run.

Mine crashed with a different message...

Can't merge models with two different entities named 'Ingredient'

Which triggered a memory that I was required to change this line of code,

managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    

to these two lines of code...

NSURL *modelURL = [[NSBundle mainBundle] URLForResource:classPersistentStore withExtension:@"momd"];
managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:self.modelURL];

This loads without error for me.

Please follow these steps - should also work for you.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top