kev*_*inl 17 iphone core-data objective-c ios
我正在研究一个iOS项目,该项目使用不同版本的coredata来处理迁移.
我还尝试在catch中包含if语句,并返回sqlite错误代码522.
这里有什么不对吗?
我的以下代码:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator{
if (__persistentStoreCoordinator != nil) {
return __persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory]
URLByAppendingPathComponent:@"coredatadb.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]){
[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil];
[__persistentStoreCoordinator release];
__persistentStoreCoordinator = nil;
return [self persistentStoreCoordinator];
}
return __persistentStoreCoordinator;
Run Code Online (Sandbox Code Playgroud)
Mac*_*ark 41
通过更改日记模式,您只能避免此问题,但您无法解决问题.新的日记模式为您的应用程序提供了诸如速度等一些好处.
真正的问题是您只删除1个文件而不是该模式中使用的所有3个文件.不仅有你的coredatadb.sqlite,还有coredatadb.sqlite-shm和coredatadb.sqlite-wal文件.有关架构和预写的事情,请查看Core Data上的WWDC 2013视频以获取详细信息.
要解决您的问题,您应该删除Documents目录中以"coredatadb.sqlite"开头的所有文件,一切都是免费且容易的;-)
iOS 9更新:现在使用destroyPersistentStoreAtURL或replacePersistentStoreAtURL更容易,更安全.请参阅WWDC 2015会话220_hd_whats_new_in_core_data.
因此,看起来以下解决了我的具体问题,尽管有些人会建议不要更改 sqlite 数据库上的日志模式:
// Change journal mode from WAL to MEMORY
NSDictionary *pragmaOptions = [NSDictionary dictionaryWithObject:@"MEMORY" forKey:@"journal_mode"];
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,
pragmaOptions, NSSQLitePragmasOption, nil];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4651 次 |
| 最近记录: |