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

问题描述:

当我想创建持久存储时出现错误。 当我为iOS 5.0构建时,没有错误,核心数据成功运行。核心数据 - 持久存储无法在iOS 5.1上创建

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

+ (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 

任何人知道如何解决这个错误吗?

+0

尝试从Simulator/Device中删除应用程序并再次运行,这可能会解决您的问题。谢谢 – SALMAN 2012-07-15 14:23:38

的 “解决方案” 是,.sqlite文件必须具有相同的名称作为项目名称!

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

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

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

OR

你可以通过这个链接

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

感谢

+0

没有帮助你的提示...为解决方案看到我的答案 – 2012-07-17 11:49:25