核心数据 - 无法在iOS 5.1上创建持久存储

时间:2012-07-15 14:17:48

标签: ios5 core-data persistence

当我想创建持久存储时,我收到错误。 当我为iOS 5.0构建时,没有错误,核心数据也能成功运行。

以下是创建持久存储的代码:

+ (NSManagedObjectContext *)getContext {

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


    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSURL *storeUrl = [NSURL fileURLWithPath:[basePath stringByAppendingFormat:@"Database.sqlite"]];
    NSPersistentStoreCoordinator *persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[NSManagedObjectModel mergedModelFromBundles:nil]];
    NSError *error = nil;

    if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
        NSLog(@"error loading persistent store..");
        [[NSFileManager defaultManager] removeItemAtPath:storeUrl.path error:nil];
        if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
            NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
            //abort();
        }
    }


    NSManagedObjectContext *context = [[NSManagedObjectContext alloc] init];
    [context setPersistentStoreCoordinator:persistentStoreCoordinator];

    return context;


}

错误日志:

error loading persistent store..
2012-07-15 13:16:31.440 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xde88ca0 {reason=Failed to create file; code = 1}, {
    reason = "Failed to create file; code = 1";
}
2012-07-15 13:16:31.455 Partyfinder[8213:707] error loading persistent store..
2012-07-15 13:16:31.458 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xdeb5360 {reason=Failed to create file; code = 1}, {
    reason = "Failed to create file; code = 1";
}
2012-07-15 13:16:31.467 Partyfinder[8213:707] error loading persistent store..
2012-07-15 13:16:31.470 Partyfinder[8213:707] Unresolved error Error Domain=NSCocoaErrorDomain Code=512 "The operation couldn’t be completed. (Cocoa error 512.)" UserInfo=0xdeb6680 {reason=Failed to create file; code = 1}, {
    reason = "Failed to create file; code = 1";
}
2012-07-15 13:16:31.483 Partyfinder[8213:707] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores.  It cannot perform a save operation.'
*** First throw call stack:
(0x358fa88f 0x37ca1259 0x36230fe7 0x36299287 0x593e5 0x45917 0x5bfc3 0x5c487 0x5bfc3 0x5bf6b 0x45285 0x5bfc3 0x5a883 0x5e26b 0x358ca433 0x3588f533 0x3588f77b 0x358cead3 0x358ce29f 0x358cd045 0x358504a5 0x3585036d 0x374ec439 0x3335ccd5 0x42119 0x420c4)
terminate called throwing an exception

任何人都知道如何解决错误?

2 个答案:

答案 0 :(得分:0)

我认为此错误有两种可能的情况:

1)您之前执行的应用程序错误地创建了该文件。如果您使用菜单重置模拟器,这将解决:iOS模拟器>重置内容和设置,并从您的设备卸载应用程序:长按>点击x符号。

2)您的应用中的其他地方有一些代码可以创建此文件。如果是这种情况,您应该找到此代码并将其删除。

OR

您可以使用此链接

Core Data Tips for iPhone Devs Part 2: Better Error Messages

由于

答案 1 :(得分:0)

“解决方案”是.sqlite文件必须与项目名称同名!