3

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,我应该在哪里传递配置选项?

4

2 回答 2

2

据我了解,RestKit 位于 Core Data 之上。因此,即使您使用种子数据库并让 RestKit 为对象管理器分配对象存储,也会使用 Core Data 提供的 sqlite 数据库。

要使用 RestKit 启用轻量级迁移,您可以使用- (NSPersistentStoreCoordinator *)persistentStoreCoordinatorAppDelegate 中的方法(请参阅此线程

应用委托

- (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();
    }
}
于 2012-07-31T20:01:11.550 回答
2

RKManagedObjectStore.m您可以在, increatePersistentStoreCoordinator方法中添加这些选项。

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 个新实体并重命名了一个旧实体,并且在不从设备中删除以前的应用程序的情况下运行完美。希望这对您有所帮助。

于 2013-05-31T12:27:19.893 回答