使用 Core Data
在 XCode 中编译和运行时,我遇到了以前从未见过的错误:
2013-09-12 16:59:10.156 myapp[57811:70b] CoreData: error:
(14) I/O error for database at /Users/administrador/Library/
Application Support/iPhone Simulator/7.0/Applications/
6BA67336-B093-46CF-8B11-E3595409DAC2/myapp.app/database.sqlite.
SQLite error code:14, 'unable to open database file'
生成此消息的代码是:
psc = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:self.managedObjectModel];
NSURL *storeURL = [[NSBundle mainBundle]
URLForResource:@"database" withExtension:@"sqlite"];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil URL:storeURL
options:@{NSReadOnlyPersistentStoreOption : @YES} error:NULL];
我已经尝试过Build->Clean
,删除派生数据,卸载该应用程序。
我在发布之前检查了这个问题,我相信问题是不同的。
注意:sqlite 是应用程序的资源
使用调试建议的信息
2013-09-12 17:43:38.341 myapp[58322:70b] CoreData: annotation: Connecting to sqlite database file at "/Users/administrador/Library/Application Support/iPhone Simulator/7.0/Applications/6BA67336-B093-46CF-8B11-E3595409DAC2/myapp.app/database.sqlite"
2013-09-12 17:43:38.360 myapp[58322:70b] CoreData: sql: SELECT Z_VERSION, Z_UUID, Z_PLIST FROM Z_METADATA
2013-09-12 17:43:38.363 myapp[58322:70b] CoreData: annotation: Disconnecting from sqlite database due to an error.
2013-09-12 17:43:38.364 myapp[58322:70b] CoreData: error: (14) I/O error for database at /Users/administrador/Library/Application Support/iPhone Simulator/7.0/Applications/6BA67336-B093-46CF-8B11-E3595409DAC2/myapp.app/database.sqlite. SQLite error code:14, 'unable to open database file'
2013-09-12 17:43:38.366 myapp[58322:70b] CoreData: annotation: Disconnecting from sqlite database.
现在 iOS7 上的 NDA 已被解除,为了完成起见,我可以发布我为此问题找到的解决方法。
iOS7 中的核心数据默认使用WAL
sqlite
。
唯一有效的解决方案是使用 iOS6 模拟器创建 sqlite,无需WAL
并将其导入项目中:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
static NSPersistentStoreCoordinator *psc;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
psc = [[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:self.managedObjectModel];
NSURL *storeURL = [[NSBundle mainBundle]
URLForResource:@"database" withExtension:@"sqlite"];
[psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:@{NSReadOnlyPersistentStoreOption : @YES,
NSSQLitePragmasOption: @{@"journal_mode":@"DELETE"}}
error:NULL];
});
return psc;
}
NSURL *storeURL = [[NSBundle mainBundle]
URLForResource:@"database" withExtension:@"sqlite"];
在应用程序捆绑包中构建一个只读路径。持久存储文件需要驻留在可写目录中,例如"文档"目录。
编辑:上面的答案实际上是错误的,可以从应用程序包中打开只读核心数据文件(使用NSReadOnlyPersistentStoreOption
)。我目前唯一能想象的是捆绑文件不是有效的核心数据数据库。添加启动参数
-com.apple.CoreData.SQLDebug 1
可能有助于定位问题。