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

发布于 2024-12-27 11:16:01 字数 306 浏览 4 评论 0原文

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

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

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

My plan is to delete the old Core Data stack (the NSManagedObjectModel .momd file & the NSPersistentStore .sqlite file) because:

  • I don't have experience with Core Data migrations.
  • the new .xcdatamodel schema is completely different than the old one.
  • I can safely delete the user's old data because it's all stored on our server and the new app downloads the latest data from our server anyway.

In this case, is complete deletion the best way to go about migration?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断念 2025-01-03 11:16:01

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

从技术上讲,这是一件微不足道的事情。当您设置 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];

It is a perfectly valid thing to do if your app requires internet access anyway. Otherwise users may be left with an empty data set (you delete the old database when you find it's incompatible with the current model, but you cannot re-populate it without access to the server).

Technically, that's a trivial thing to do. When you set up the 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];
宁愿没拥抱 2025-01-03 11:16:01

如果您创建一个空白的 Core Data 应用程序,您可以在应用程序委托中的 Apple 注释中找到必要的代码:

如果您在开发过程中遇到架构不兼容错误,您
可以通过以下方式降低频率:

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

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

If you create a blank Core Data application you find the necessary code in Apples comments in the Application Delegate:

If you encounter schema incompatibility errors during development, you
can reduce their frequency by:

  • Simply deleting the existing store: [[NSFileManager defaultManager] removeItemAtURL:storeURL error:nil]

  • Performing automatic lightweight migration by passing the following dictionary as the options
    parameter:@{NSMigratePersistentStoresAutomaticallyOption:@YES,
    NSInferMappingModelAutomaticallyOption:@YES}
    Lightweight migration will only work for a limited set of schema changes; consult "Core Data Model Versioning and Data Migration
    Programming Guide" for details.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文