问题 核心数据迁移:如何删除核心数据堆栈?


我的计划是删除旧的核心数据堆栈( NSManagedObjectModel  .momd 文件和 NSPersistentStore  .sqlite 文件)因为:

  • 我没有Core Data迁移的经验。
  • 新的.xcdatamodel架构与旧架构完全不同。
  • 我可以安全地删除用户的旧数据,因为它全部存储在我们的服务器上,新应用程序无论如何都会从我们的服务器下载最新数据。

在这种情况下,是否完全删除了迁移的最佳方法?


5766
2018-01-16 20:47


起源



答案:


如果您的应用程序需要互联网访问,这是完全有效的事情。否则,用户可能会留下一个空数据集(当您发现它与当前模型不兼容时删除旧数据库,但如果不访问服务器则无法重新填充它)。

从技术上讲,这是一件微不足道的事情。当你设置 NSPersistentStoreCoordinator

NSURL *storeURL = ...;
NSManagedObjectModel *managedObjectModel = ...;
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];

// Check if we already have a persistent store
if ( [[NSFileManager defaultManager] fileExistsAtPath: [storeURL path]] ) {
    NSDictionary *existingPersistentStoreMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType: NSSQLiteStoreType URL: storeURL error: &error];
    if ( !existingPersistentStoreMetadata ) {
        // Something *really* bad has happened to the persistent store
        [NSException raise: NSInternalInconsistencyException format: @"Failed to read metadata for persistent store %@: %@", storeURL, error];
    }

    if ( ![managedObjectModel isConfiguration: nil compatibleWithStoreMetadata: existingPersistentStoreMetadata] ) {
        if ( ![[NSFileManager defaultManager] removeItemAtURL: storeURL error: &error] )
            NSLog(@"*** Could not delete persistent store, %@", error);
    } // else the existing persistent store is compatible with the current model - nice!
} // else no database file yet

[_persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType 
                                          configuration: nil 
                                                    URL: storeURL 
                                                options: nil 
                                                  error: &error];

12
2018-01-17 06:03



我收到此错误SQLite错误代码:11,'更新后的初始启动时'数据库磁盘映像格式错误'。但是在随后的发布中,它不是一个问题,因为sqlite文件被删除了。但似乎我不能避免这种一次性的应用程序崩溃。并且感觉应用程序在更新后立即崩溃有点令人失望。有任何想法吗 ?? - raw3d


答案:


如果您的应用程序需要互联网访问,这是完全有效的事情。否则,用户可能会留下一个空数据集(当您发现它与当前模型不兼容时删除旧数据库,但如果不访问服务器则无法重新填充它)。

从技术上讲,这是一件微不足道的事情。当你设置 NSPersistentStoreCoordinator

NSURL *storeURL = ...;
NSManagedObjectModel *managedObjectModel = ...;
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: managedObjectModel];

// Check if we already have a persistent store
if ( [[NSFileManager defaultManager] fileExistsAtPath: [storeURL path]] ) {
    NSDictionary *existingPersistentStoreMetadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType: NSSQLiteStoreType URL: storeURL error: &error];
    if ( !existingPersistentStoreMetadata ) {
        // Something *really* bad has happened to the persistent store
        [NSException raise: NSInternalInconsistencyException format: @"Failed to read metadata for persistent store %@: %@", storeURL, error];
    }

    if ( ![managedObjectModel isConfiguration: nil compatibleWithStoreMetadata: existingPersistentStoreMetadata] ) {
        if ( ![[NSFileManager defaultManager] removeItemAtURL: storeURL error: &error] )
            NSLog(@"*** Could not delete persistent store, %@", error);
    } // else the existing persistent store is compatible with the current model - nice!
} // else no database file yet

[_persistentStoreCoordinator addPersistentStoreWithType: NSSQLiteStoreType 
                                          configuration: nil 
                                                    URL: storeURL 
                                                options: nil 
                                                  error: &error];

12
2018-01-17 06:03



我收到此错误SQLite错误代码:11,'更新后的初始启动时'数据库磁盘映像格式错误'。但是在随后的发布中,它不是一个问题,因为sqlite文件被删除了。但似乎我不能避免这种一次性的应用程序崩溃。并且感觉应用程序在更新后立即崩溃有点令人失望。有任何想法吗 ?? - raw3d


如果您创建一个空白的Core Data应用程序,您可以在Application Delegate中的Apples注释中找到所需的代码:

如果您在开发过程中遇到架构不兼容错误,那么您   可以减少他们的频率:

  • 只需删除现有商店:[[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

  • 通过传递以下字典作为选项来执行自动轻量级迁移   参数:@ {NSMigratePersistentStoresAutomaticallyOption:@YES,   NSInferMappingModelAutomaticallyOption:@YES}         轻量级迁移仅适用于一组有限的架构更改;请参阅“核心数据模型版本控制和数据迁移”   编程指南“了解详情。


1
2017-10-13 16:30