【发布时间】:2014-04-30 04:38:30
【问题描述】:
我正在为我的应用程序使用位于 MainBundle 中的只读 sqlite 数据库。在 PersistentStoreCoordinator 中,我正在使用以下代码加载数据库:
NSDictionary *storeOptions = @{NSReadOnlyPersistentStoreOption : [NSNumber numberWithBool:YES]};
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self applicationManagedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:[[NSBundle mainBundle] URLForResource:@"applications" withExtension:@"sqlite"] options:nil error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error description]);
abort();
}
在 iOS7 中,此代码在模拟器和设备中都崩溃,并出现以下错误:
CoreData: error: (14) I/O error for database at /var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/myapp.app/applications.sqlite. SQLite error code:14, 'unable to open database file'
2014-03-22 20:45:34.346 GfxHotkeys3[1369:60b] CoreData: error: Encountered exception I/O error for database at /var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/myapp.app/applications.sqlite. SQLite error code:14, 'unable to open database file' with userInfo {
NSFilePath = "/var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/myapp.app/applications.sqlite";
NSSQLiteErrorDomain = 14;
} while checking table name from store: <NSSQLiteConnection: 0x155b0fd0>
2014-03-22 20:45:34.372 GfxHotkeys3[1369:60b] Unresolved error Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x155b01b0 {NSUnderlyingException=I/O error for database at /var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/myapp.app/applications.sqlite. SQLite error code:14, 'unable to open database file', NSSQLiteErrorDomain=14}, Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x155b01b0 {NSUnderlyingException=I/O error for database at /var/mobile/Applications/4B81AEFE-03E6-4156-B52D-3452515FACAF/GfxHotkeys3.app/applications.sqlite. SQLite error code:14, 'unable to open database file', NSSQLiteErrorDomain=14}
在商店选项中传递 nil,将在模拟器中运行应用程序,但不在设备上运行(因为它不能在 MainBundle 上写入)。
我可以通过将数据库复制到文档中并从那里加载来解决这个问题,但我想知道为什么会这样。我多年来一直使用这个选项从 MainBundle 加载只读 sqlite,但在 iOS 7 中崩溃了......
有什么线索吗?
非常感谢
【问题讨论】: