【问题标题】:persistentstorecoordinator sqlite error code:522 'not an error'persistentstorecoordinator sqlite 错误代码:522 '不是错误'
【发布时间】:2013-08-16 15:34:35
【问题描述】:

我正在开发一个 iOS 项目,该项目使用不同版本的 coredata 处理迁移。

我还尝试将 if 语句包含在 catch 中,它返回一个 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;

【问题讨论】:

    标签: iphone ios objective-c core-data


    【解决方案1】:

    通过更改日志模式,您只能避免问题,但不能解决问题。新的日记模式可为您的应用带来一些好处,例如速度。

    真正的问题是您只删除了 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 会议 220_hd_whats_new_in_core_data。

    【讨论】:

    【解决方案2】:

    所以看起来以下解决了我的具体问题,尽管有些人会建议不要更改 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];
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-03
    • 2015-09-01
    • 2016-10-24
    • 2017-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多