cocoa-touch - 使用 RestKit 进行轻量级迁移

标签 cocoa-touch ios5 core-data migration restkit

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/

相关文章:

objective-c - 获取一个月的最后一天

ios - 在 Swift 中使用 RestKit 和 CoreData

iphone - 切换到 map 应用后返回同一个应用

ios - 在 UITableView 中显示 Coredata

iOS:检查 coredata 对象是否仍然存在?

iphone - 在 Objective-C 中将 TypeDef 作为参数

ios - 如何从 Cocoa Touch 框架访问窗口或 Root View Controller ?

iphone - 带有 UIActivityIndi​​catorView 的事件指示器(微调器)

objective-c - IOS 中的圆形进度条

iphone - 解析 JSON Feed iOS 5