与RestKit轻量级迁移
苹果的文档给了设立(自动)轻量级迁移下面的例子:与RestKit轻量级迁移
NSError *error = nil;
NSURL *storeURL = <#The URL of a persistent store#>;
NSPersistentStoreCoordinator *psc = <#The coordinator#>;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
BOOL success = [psc addPersistentStoreWithType:<#Store type#>
configuration:<#Configuration or nil#> URL:storeURL
options:options error:&error];
if (!success) {
// Handle the error.
}
但是我使用RestKit,它处理幕后的持续性商店的创建。我的初始化代码的简化版本是这样的:
// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURL:rootURL];
// Create the object store
objectManager.objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:databaseName
usingSeedDatabaseName:seedDatabaseName
managedObjectModel:nil //Don't need to pass it in. It is infered
delegate:self];
// Create Mappings
...
// Define Relationships
...
// Set Mappings
...
我应该在哪里传递给该RestKit创建persistantStore幕后配置选项?
在我的理解中,RestKit位于Core Data之上。因此,即使在使用种子数据库并让RestKit为对象管理器分配对象存储时,也会使用Core Data提供的sqlite数据库。
要启用具有RestKit轻量级迁移,你可以使用在AppDelegate中的- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
方法(见本thread)
的AppDelegate
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil) return __persistentStoreCoordinator;
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"thenameofyoursqlite.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
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])
{
NSLog(@"Auto migration failed, error %@, %@", error, error.userInfo);
abort();
}
}
可以在RKManagedObjectStore.m
添加这些选项,在createPersistentStoreCoordinator
方法。
对于RestKit的版本0.10,它已被添加,不确定是否为最新版本。但如果尚未添加,您可以添加自己。该方法的最终外观是这样的。
- (void)createPersistentStoreCoordinator
{
NSAssert(_managedObjectModel, @"Cannot create persistent store coordinator without a managed object model");
NSAssert(!_persistentStoreCoordinator, @"Cannot create persistent store coordinator: one already exists.");
NSURL *storeURL = [NSURL fileURLWithPath:self.pathToStoreFile];
NSError *error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:_managedObjectModel];
// Allow inferred migration from the original version of the application.
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]) {
if (self.delegate != nil && [self.delegate respondsToSelector:@selector(managedObjectStore:didFailToCreatePersistentStoreCoordinatorWithError:)]) {
[self.delegate managedObjectStore:self didFailToCreatePersistentStoreCoordinatorWithError:error];
} else {
NSAssert(NO, @"Managed object store failed to create persistent store coordinator: %@", error);
}
}
}
我已经在我的项目测试了这个,通过增加3个新的实体和重命名旧的实体,并没有从设备删除以前的应用程序正在完善。 希望这可以帮助你。
我正在使用RestKit 0.23.1,并且在addSQLitePersistentStoreAtPath中默认实现了这些选项:fromSeedDatabaseAtPath:withConfiguration:options:error:error:RKManagedObjectStore的方法,即使我为选项参数传递nil,RestKit默认包含所需的键为轻量级迁移,耶! – DonnaLea 2014-06-27 20:59:27
您好@Pedr,我自己正在寻找轻量级迁移并遇到问题,您是否成功实现了轻量级迁移? – Khawar 2013-05-31 05:30:03
对不起@Khawar。真的不记得了。 – Undistraction 2013-05-31 09:43:19
没问题我已修复此问题,thanx – Khawar 2013-05-31 11:10:06