CoreData 轻量级迁移
Core Data 的轻量级迁移可以处理对数据模型进行的简单更改,例如将新属性添加到现有实体中,轻量级迁移基本上与普通迁移相同,区别就是不需要映射模型,因为Core Data可以从数据模型推断映射.
在轻量级迁移过程中,Core Data再NSBundle allBundles 结果返回的捆绑包中查找模型.出于这个原因,常见的做法是将数据模型存储再应用捆绑包中.
通过生成推断的映射模型执行轻量级迁移,模型更改必须满足以下条件:
1. 将新属性添加到现在实体中
2. 从实体中删除属性
3. 将属性从可选更改为非可选,反之亦然
4. 重命名实体
5. 重命名特性
6. 添加新关系
7. 删除现有关系
8. 重命名关系
重命名实体,关系或属性时,需要使用属性检测器将目标模型中指定的新名称设置为对应对象再源模型中的名称.
// CoreData 轻量级迁移
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator { if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
} _persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreData_LightweightMigration.sqlite"];
NSError *error = nil;
NSString *failureReason = @"There was an error creating or loading the application's saved data."; /*! 主要添加的代码块*/
// NSMigratePersistentStoresAutomaticallyOption
// NSInferMappingModelAutomaticallyOption
// 设置为YES
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,[NSNumber numberWithBool:YES],NSInferMappingModelAutomaticallyOption, nil]; if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) { NSMutableDictionary *dict = [NSMutableDictionary dictionary];
dict[NSLocalizedDescriptionKey] = @"Failed to initialize the application's saved data";
dict[NSLocalizedFailureReasonErrorKey] = failureReason;
dict[NSUnderlyingErrorKey] = error;
error = [NSError errorWithDomain:@"YOUR_ERROR_DOMAIN" code: userInfo:dict]; NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
} // 去实现自动迁移,在运行时 Core Data必须能够发现它自己源和目的管理对象模型.如果你需要取把你的模型到本地,不通过自动检查发现,然后你需要去生成推测模型和初始化这个迁移你自己使用一个迁移管理(NSMigrationManager 实例) // 下面的简单代码简单的说明了怎么生成一个推测模型和使用迁移管理初始化一个迁移,这个代码假设你已经实现了两个方法 sourceModel和destinationModel , 分别的返回数据源和目的管理对象模型.
- (BOOL)migrateStore:(NSURL *)storeURL toVersionTwoStore:(NSURL *)dstStoreURL error:(NSError **)outError { // 1.创建原模型
NSManagedObjectModel * sourceModel = [NSManagedObjectModel mergedModelFromBundles:nil]; // 2.创建目的模型
NSManagedObjectModel * destinationModel = [NSManagedObjectModel mergedModelFromBundles:nil]; // Try to get an inferred mapping model.
// 3.尝试获得一个自动推测模型
NSMappingModel *mappingModel = [NSMappingModel inferredMappingModelForSourceModel:sourceModel destinationModel:destinationModel error:outError]; // If Core Data cannot create an inferred mapping model, return NO.
// 4.如果推测模型为空则返回NO
if (!mappingModel) {
return NO;
} // Create a migration manager to perform the migration.
// 5.创建一个推测管理实现迁移
NSMigrationManager *manager = [[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel]; // 6. 管理迁移库从从储存URL到目的URL
BOOL success = [manager migrateStoreFromURL:storeURL type:NSSQLiteStoreType
options:nil withMappingModel:mappingModel toDestinationURL:dstStoreURL
destinationType:NSSQLiteStoreType destinationOptions:nil error:outError]; return success;
}