检测轻量级核心数据迁移

had*_*zoo 21 iphone core-data iphone-sdk-3.0 core-data-migration

我正在成功使用Core Data的自动轻量级迁移.但是,当在迁移期间创建特定实体时,我想用一些数据填充它.当然,我可以在每次应用程序启动时检查实体是否为空,但是当Core Data具有迁移框架时,这似乎效率低下.

是否可以检测轻量级迁移何时发生(可能使用KVO或通知),还是需要实现标准迁移?

我尝试过使用它NSPersistentStoreCoordinatorStoresDidChangeNotification,但是在迁移发生时它不会触发.

had*_*zoo 58

要检测是否需要迁移,请检查持久性存储协调器的托管对象模型是否与现有商店的元数据兼容(改编自Apple的Is Migration Necessary):

NSError *error = nil;
persistentStoreCoordinator = /* Persistent store coordinator */ ;
NSURL *storeUrl = /* URL for the source store */ ;

// Determine if a migration is needed
NSDictionary *sourceMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:NSSQLiteStoreType
                                                                                          URL:storeUrl
                                                                                        error:&error];
NSManagedObjectModel *destinationModel = [persistentStoreCoordinator managedObjectModel];
BOOL pscCompatibile = [destinationModel isConfiguration:nil compatibleWithStoreMetadata:sourceMetadata];
NSLog(@"Migration needed? %d", !pscCompatibile);
Run Code Online (Sandbox Code Playgroud)

如果pscCompatibileNO,则需要进行迁移.要检查实体更改,NSStoreModelVersionHashes请将sourceMetadata字典中的键与以下内容进行比较[destinationModel entities]:

NSSet *sourceEntities = [NSSet setWithArray:[(NSDictionary *)[sourceMetadata objectForKey:@"NSStoreModelVersionHashes"] allKeys]];
NSSet *destinationEntities = [NSSet setWithArray:[(NSDictionary *)[destinationModel entitiesByName] allKeys]];

// Entities that were added
NSMutableSet *addedEntities = [NSMutableSet setWithSet:destinationEntities];
[addedEntities minusSet:sourceEntities];

// Entities that were removed
NSMutableSet *removedEntities = [NSMutableSet setWithSet:sourceEntities];
[removedEntities minusSet:destinationEntities];

NSLog(@"Added entities: %@\nRemoved entities: %@", addedEntities, removedEntities);
Run Code Online (Sandbox Code Playgroud)


Pau*_*ing 5

接受的答案转换为 Swift ...

  var persistentStoreCoordinator: NSPersistentStoreCoordinator?
  var url: URL
  do {
    let sourceMetadata = try NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType, at: url, options: nil)
    if let destinationModel = persistentStoreCoordinator?.managedObjectModel {
      let compatibile = destinationModel.isConfiguration(withName: nil, compatibleWithStoreMetadata: sourceMetadata)
      if !compatibile {
        if let versionHashes = sourceMetadata["NSStoreModelVersionHashes"] as? [String: Any] {
          let sourceEntities = Set(versionHashes.keys)
          let destinationEntities = Set(destinationModel.entitiesByName.keys)

          var addedEntities = Set(destinationEntities)
          addedEntities.subtract(sourceEntities)

          var removedEntities = Set(sourceEntities)
          removedEntities.subtract(destinationEntities)
          let modelName = (destinationModel.versionIdentifiers.first as? String) ?? ""
          NSLog("Core Data requires a migration to model '\(modelName)'...\nAdded: \(addedEntities)\nRemoved: \(removedEntities)")
        }
      }
    }
  } catch {
            ...
  }
Run Code Online (Sandbox Code Playgroud)