Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 核心数据迁移:如何删除核心数据堆栈?_Iphone_Objective C_Ios_Core Data_Core Data Migration - Fatal编程技术网

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

Iphone 核心数据迁移:如何删除核心数据堆栈?,iphone,objective-c,ios,core-data,core-data-migration,Iphone,Objective C,Ios,Core Data,Core Data Migration,我的计划是删除旧的核心数据堆栈(NSManagedObjectModel.momd文件和NSPersistentStore.sqlite文件),因为: 我没有核心数据迁移的经验 新的.xcdatamodel模式与旧模式完全不同 我可以安全地删除用户的旧数据,因为这些数据都存储在我们的服务器上,而且新应用程序会从我们的服务器下载最新的数据 在这种情况下,完全删除是迁移的最佳方式吗?如果你的应用程序需要访问互联网,那么这样做是完全有效的。否则,用户可能会留下一个空的数据集(当您发现旧数据库与当前

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

  • 我没有核心数据迁移的经验
  • 新的.xcdatamodel模式与旧模式完全不同
  • 我可以安全地删除用户的旧数据,因为这些数据都存储在我们的服务器上,而且新应用程序会从我们的服务器下载最新的数据

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

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

从技术上讲,这是一件小事。设置
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];

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

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

  • 只需删除现有存储:[[NSFileManager defaultManager]RemoveItemAttribute:storeURL错误:nil]

  • 通过将以下字典作为选项传递,执行自动轻量级迁移 参数:@{NSMigratePersistentStoresAutomaticallyOption:@是, nsInFermappingModelAutomatically选项:@YES} 轻量级迁移只适用于一组有限的模式更改;请参阅“核心数据模型版本控制和数据迁移” 有关详细信息,请参阅“编程指南”


我收到此错误SQLite错误代码:11,“数据库磁盘映像格式不正确”,在更新后的初始启动时。但是在随后的启动中,由于sqlite文件被删除,所以这并不是一个问题。但我似乎无法避免这一次的应用程序崩溃。而且,在更新后立即感到应用程序崩溃是一种令人失望的感觉。有什么想法吗??