Apple 的文档给出了以下设置(自动)轻量级迁移的示例:
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 )
应用委托(delegate)
- (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();
}
}
关于cocoa-touch - 使用 RestKit 进行轻量级迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11735712/