开发者

CoreData could not fulfill a fault when adding new attribute

I am receiving a "CoreData could not fulfill a fault for ..." error message when trying to access a new attribute in a new data model. If I work with new data I'm ok, but when I attempt to read existing data I get the error. Do I need to handle the entity differently myself if the attribute isn't in my original data? I was under the impression that Core Data could handle this for me. My new attribute is marked as optional with a default value.

I have created a new .xcdatamodel (and set it to be the current version) and updated my NSPersistentStoreCoordinator initialization to take advantage of the lightweight 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: Afte开发者_高级运维r more digging I've updated my managedObjectModel to:

- (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;
}

This still hasn't resolved my problems. I've clean and rebuilt, but still no love.


How are you constructing your NSManagedObjectModel? If you are passing it a specific file that might be causing your issue as you may be loading the older, original mom file that is lingering around your project. Ideally you should be now loading the momd bundle or just loading all compiled models from your bundle using:

NSManagedObjectModel *model = [NSManagedObjectModel mergedModelFromBundles:nil];

Which, if you receive an error, indicates that you need to clean your project to get rid of stale compiled models.

Update

Since it is not an old model issue, we move onto the next possibility. That the version is not set correctly in the plist. To check this, use finder or terminal and look inside of the momd bundle and open the plist therein. Check that to confirm that the new model is indeed set as the current version.

Assuming that does not work, next run your app in the simulator and have it save immediately upon creation of the MOC. After that, open the sqlite3 file using the command line tool and check the schema to see if it has updated to the new structure.

Assuming that is set correctly, are you using custom NSManagedObject subclasses?


Turns out that there was no problem with the versioning. I had some rather (too) complex logic which removed my object from the model and then I later tried to access it.

+1 to Marcus for the additional debugging pointers, they will no doubt come in handy at some point.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜