【问题标题】:CoreData: error: (14) on device, not in simulatorCoreData:错误:(14)在设备上,不在模拟器中
【发布时间】:2014-08-12 02:09:02
【问题描述】:

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

我在模拟器上也遇到了同样的错误,并从这个线程实现了解决方案: enter link description here

使用此代码:

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

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

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'";

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

部署目标:6.1

【问题讨论】:

  • 写完我的答案后,我看到您在问题中添加了相同的代码作为评论:// NSDictionary *options = @{NSReadOnlyPersistentStoreOption : @YES, NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };。 - 那么你现在的实际代码是什么?
  • 我假设 SQLite 文件与应用程序捆绑在一起。 创建数据库时,您是否也禁用了 WAL 模式?
  • @MartinR 不,我在创建数据库时没有禁用 WAL,现在将对其进行测试。
  • @MartinR 解决了这个问题,非常感谢您的帮助。请将此添加为答案,以便我将其标记为已解决。我仍然不明白为什么它在模拟器中有效,但在设备中无效。
  • 不客气……完成了。

标签: ios core-data xcode5


【解决方案1】:

你的代码

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:1]
                                                forKey:NSReadOnlyPersistentStoreOption];
options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };

options 设置为仅包含NSSQLitePragmasOption 键的字典,但不包含 NSReadOnlyPersistentStoreOption 键,因为第二行覆盖了 字典分配在第一行。

你可能想要的是

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

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

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

模拟器中没有出现问题,因为应用程序目录是 在模拟器中可读写,但在设备上为只读。

【讨论】:

  • 谢谢,不幸的是这次我在模拟器中遇到了同样的错误:-(
  • MartinR 的评论,这是解决方案:我假设 SQLite 文件与应用程序捆绑在一起。创建数据库时是否还禁用了 WAL 模式? – Martin R 2 小时前
猜你喜欢
  • 1970-01-01
  • 2011-06-20
  • 1970-01-01
  • 2015-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-01
相关资源
最近更新 更多