CoreData Failed To Fail To Add New Attribute

I get the error "CoreData cannot complete the error for ..." when trying to access a new attribute in a new data model. If I work with new data, I am fine, but when I try to read existing data, I get an error. Do I need to handle the object differently if the attribute is not in my source data? I got the impression that Core Data could handle this for me. My new attribute is marked as optional with a default value.

I created a new .xcdatamodel (and installed it as the current version) and updated my initialization of NSPersistentStoreCoordinator to use light migration as follows:

NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:      
         [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
         [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];

NSError *error = nil;
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] 
          initWithManagedObjectModel:[self managedObjectModel]];

if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType 
            configuration:nil URL:storeUrl options:options error:&error]) 
{
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
 }    

Any help is appreciated.

UPDATE: my managedObjectModel, :

- (NSManagedObjectModel *)managedObjectModel {

    if (managedObjectModel != nil) {
        return managedObjectModel;
    }
    //managedObjectModel = [[NSManagedObjectModel mergedModelFromBundles:nil] retain];    

    NSString *path = [[NSBundle mainBundle] pathForResource:@"< MyModel >" ofType:@"momd"];
    NSURL *momURL = [NSURL fileURLWithPath:path];
    managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

    return managedObjectModel;
}

. , .

+3
2

, . , , .

+1 , , , - .

+2

NSManagedObjectModel? , , , , . momd , :

NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];

, , , , .

Update

, . , ​​ plist. , momd . , , ​​ .

, , MOC. sqlite3 , , ​​ .

, , NSManagedObject?

+2

Source: https://habr.com/ru/post/1736562/


All Articles