【发布时间】: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 解决了这个问题,非常感谢您的帮助。请将此添加为答案,以便我将其标记为已解决。我仍然不明白为什么它在模拟器中有效,但在设备中无效。
-
不客气……完成了。