我在设备上运行时遇到此错误,而不是模拟器(现在)。
我在模拟器上也遇到了同样的错误,并从这个线程实现了解决方案: 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
答案 0 :(得分:4)
您的代码
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模式 与应用程序捆绑在一起。
模拟器中不会出现此问题,因为应用程序目录是 在模拟器中读写,但在设备上只读。