核心数据迁移:如何删除核心数据堆栈? [英] Core Data Migration: How to delete the Core Data stack?

查看:154
本文介绍了核心数据迁移:如何删除核心数据堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


  • 我没有Core Data迁移的经验。

  • 新的.xcdatamodel模式与旧的模式完全不同。

  • I可以安全地删除用户的旧数据,因为它们都存储在我们的服务器上,新应用程序始终会从我们的服务器下载最新数据。

推荐答案

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

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).

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

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];

这篇关于核心数据迁移:如何删除核心数据堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆