开发者

Saving core data results in a crash

I have a problem with my core data. I am trying to save the context and I keep getting this error:

'NSInternalInconsistencyException', reason: 'This 开发者_StackOverflow中文版NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'

This is my code where I make the core data things:

managedObjectModel_ = [NSManagedObjectModel mergedModelFromBundles:[NSArray arrayWithObject:[NSBundle mainBundle]]];

persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:managedObjectModel_];

managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:persistentStoreCoordinator_];

I really have no idea what's going on. BTW, I am developing for iOS if it makes a difference.

Thanks.


You need to add persistent store to coordinator. Something like this:

NSURL *url = [NSURL fileURLWithPath:[[self applicationDocumentDirectory] stringByAppendingPathComponent:@"MyDatabaseName.sqlite"]];

NSError *error;
if (![storeCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:nil error:&error]) {
    NSAssert1(NO, @"Adding of store to coordinator faild. %@", [error localizedDescription]);
}   
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜