iphone - persistentstorecoordinator sqlite 错误代码 :522 'not an error'

标签 iphone ios objective-c core-data

我正在开发一个 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 文件。关于模式和预写的一些东西,查看 Core Data 上的 WWDC 2013 视频了解详细信息。 [更新:它从 https://developer.apple.com/videos/play/wwdc2013/207/ 的第 40 分钟开始。视频]

要解决您的问题,您应该删除 Documents 目录中以“coredatadb.sqlite”开头的所有文件,然后一切又变得轻松简单了;-)

iOS 9 更新:现在使用 destroyPersistentStoreAtURL 或 replacePersistentStoreAtURL 更容易也更安全。请参阅 WWDC 2015 session 220_hd_whats_new_in_core_data。

关于iphone - persistentstorecoordinator sqlite 错误代码 :522 'not an error' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18277092/

相关文章:

ios - Nimbus 与使用 cocoapods 的 iOS 4.3 不兼容

ios - ALAssetsLibrary 在位置获取照片

iphone - 低内存会阻止显示模态视图 Controller 吗?

iphone - 自定义 UINavigationBar 的最惯用的 iOS5 方式?

ios - 跟踪 NSManagedObject 的属性变化

ios - 为什么collectionView单元格居中只能在一个方向起作用?

ios - CIFilter 减慢滚动速度

iPhone - 从 UIPickerView 更新标签

ios - 如何模拟在 iPad 上运行的 iPhone 应用程序?

iphone - 应用程序是否需要询问用户偏好以便将 iCloud 用于简单数据?