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

Ios CoreData:错误:(14)在设备上,不在模拟器中,ios,core-data,xcode5,Ios,Core Data,Xcode5,我在设备上运行时出现此错误,而不是在模拟器上(现在) 我在模拟器上也遇到了同样的错误,并从这个线程实现了解决方案: 使用此代码: - (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (__persistentStoreCoordinator != nil) { return __persistentStoreCoordinator; } // NSURL *storeURL = [[self app

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

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

使用此代码:

- (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]];
在模拟器中没有错误。此外,第一个错误也是新的

部署目标:6.1

您的代码

NSDictionary *options = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:1]
                                                forKey:NSReadOnlyPersistentStoreOption];
options = @{ NSSQLitePragmasOption : @{@"journal_mode" : @"DELETE"} };
options
设置为仅包含
NSSQLitePragmasOption
键但不包含 由于第二行覆盖了 在第一行指定的词典

你可能想要的是

NSDictionary *options = @{ 
    NSReadOnlyPersistentStoreOption : @YES,
    NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"}
};
创建包含两个键的
选项
目录

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

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

写下我的答案后,我看到您添加了与问题中的注释相同的代码:
//NSDictionary*options=@{nsreadonlypersistentstorepoption:@是,NSSQLitePragmasOption:@{“journal\u mode”:@“DELETE”}-那么您现在的实际代码是什么呢?我假设SQLite文件与应用程序捆绑在一起。您在创建数据库时是否也禁用了WAL模式?@MartinR否我在创建数据库时没有禁用WAL,现在将对此进行测试。@MartinR解决了问题,非常感谢您的帮助。请将此添加为答案,以便我标记它已解决。我仍然不明白为什么它在模拟器中工作,而不是在设备上。欢迎你。。。完成。谢谢,不幸的是,我这次在模拟器中遇到了相同的错误:-(MartinR的评论,这是解决方案:我假设SQLite文件与应用程序捆绑在一起。创建数据库时是否也禁用了WAL模式?–Martin R 2小时前
NSDictionary *options = @{ 
    NSReadOnlyPersistentStoreOption : @YES,
    NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"}
};