CoreData:错误:(14)在设备上,而不是在模拟器中

Pet*_*erK 2 core-data ios xcode5

在设备上运行时出现此错误,而不是模拟器(现在)。

我在模拟器上也遇到了同样的错误,并从这个线程实现了解决方案: 在此处输入链接描述

使用此代码:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) {
    return __persistentStoreCoordinator;
}

//    NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"engliah_FamQuiz.sqlite"];

//=== USE DATABASE FROM MAIN BUNDLE ===//
NSURL *storeURL = [[NSBundle mainBundle] URLForResource:kNewDB withExtension:@"sqlite"];

// Use this for source store - ensures you don't accidentally write to the entities
NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:1]
                                                     forKey:NSReadOnlyPersistentStoreOption];
// Make sure the WAL mode for Core Data is not enabled
   options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} }; <<< ADDED THIS LINE

//    NSDictionary *options = @{NSReadOnlyPersistentStoreOption : @YES, NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };

NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
Run Code Online (Sandbox Code Playgroud)

这解决了模拟器中的问题,但设备上的应用程序崩溃并出现以下错误:

2014-06-21 15:23:49.828 xxxx[30224:60b] Attempt to add read-only file at path file:///var/mobile/Applications/236047B5-0B0F-443D-ABCF-DCABA0166713/xxxx.app/yyyyy.sqlite read/write. Adding it read-only instead. This will be a hard error in the future; you must specify the NSReadOnlyPersistentStoreOption.
2014-06-21 15:23:49.838 xxxx[30224:60b] CoreData: error: (14) I/O error for database at /var/mobile/Applications/236047B5-0B0F-443D-ABCF-DCABA0166713/xxxx.app/yyyy.sqlite.  SQLite error code:14, 'unable to open database file'
2014-06-21 15:23:49.845 xxxx[30224:60b] Unresolved error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x16e48d80 {NSUnderlyingException=I/O error for database at /var/mobile/Applications/236047B5-0B0F-443D-ABCF-DCABA0166713/xxxx.app/yyyyy.sqlite.  SQLite error code:14, 'unable to open database file', NSSQLiteErrorDomain=14}, {
NSSQLiteErrorDomain = 14;
NSUnderlyingException = "I/O error for database at /var/mobile/Applications/236047B5-0B0F-443D-ABCF-DCABA0166713/xxxx.app/yyyy.sqlite.  SQLite error code:14, 'unable to open database file'";
Run Code Online (Sandbox Code Playgroud)

在模拟器中没有错误。此外,第一个错误也是新的。

部署目标:6.1

Mar*_*n R 5

你的代码

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:1]
                                                forKey:NSReadOnlyPersistentStoreOption];
options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };
Run Code Online (Sandbox Code Playgroud)

设置optionsNSSQLitePragmasOption仅包含键而不包含键的字典NSReadOnlyPersistentStoreOption,因为第二行覆盖了第一行中分配的字典。

你可能想要的是

NSDictionary *options = @{ 
    NSReadOnlyPersistentStoreOption : @YES,
    NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"}
};
Run Code Online (Sandbox Code Playgroud)

创建一个options包含两个键的目录。

此外,在创建与应用程序捆绑的 SQLite 文件时,您还必须禁用 WAL 模式。

该问题在模拟器中不会出现,因为应用程序目录在模拟器中是可读写的,而在设备上是只读的。