persistentstorecoordinator sqlite错误代码:522'不是一个错误'

我正在处理使用不同版本的coredata处理迁移的iOS项目。

我也试着围绕一个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; 

通过改变日志模式,你只能避免这个问题,但是你不能修复它。 新的日志模式为您的应用程序提供了诸如速度等一些优势。

真正的问题是你只删除了1个文件,而不是全部3个在该模式下使用的文件。 不仅有你的coredatadb.sqlite,还有一个coredatadb.sqlite-shm和coredatadb.sqlite-wal文件。 有关模式和预先写入的内容,请查看核心数据上的WWDC 2013video以了解详细信息。

要解决您的问题,您应该删除Documents目录中以“coredatadb.sqlite”开头的所有文件,而且一切都是免费且又容易的;-)

iOS 9更新:现在使用destroyPersistentStoreAtURL或replacePersistentStoreAtURL更简单,更安全。 请参阅WWDC 2015会话220_hd_whats_new_in_core_data。

所以看起来像下面解决了我的具体问题,虽然有些人会build议不要更改您的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];