相关疑难解决方法(0)

iPhone核心数据"自动轻量级迁移"

我正在尝试更新实现核心数据存储的应用程序.我正在为其中一个实体添加一个属性.

我将以下代码添加到我的委托类:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"Shoppee.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(@"Error: %@",error);
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }    

    return persistentStoreCoordinator;
}
Run Code Online (Sandbox Code Playgroud)

这来自以下网址: Doc

执行代码时出现以下错误:

2009-12-01 20:04:22.877

Shoppee [25633:207]错误:错误

Domain = NSCocoaErrorDomain Code = 134130 …

iphone core-data core-data-migration ios

64
推荐指数
5
解决办法
2万
查看次数

为核心数据(iPhone)实施"自动轻量级迁移"

当我向核心数据模型添加新属性时,我想让我的应用程序能够进行自动轻量级迁移.

在Apple的指南中,这是我能找到的主题的唯一信息:

自动轻量级迁移

要请求自动轻量级迁移,请在addPersistentStoreWithType中传递的选项字典中设置适当的标志:configuration:URL:options:error:.您需要将与NSMigratePersistentStoresAutomaticallyOption和NSInferMappingModelAutomaticallyOption键对应的值设置为YES:

NSError *error;
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];

if (![psc addPersistentStoreWithType:<#Store type#>
    configuration:<#Configuration or nil#> URL:storeURL
    options:options error:&error]) {
    // Handle the error.
}
Run Code Online (Sandbox Code Playgroud)

NSPersistentStoreCoordinator以这种方式初始化:

- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {

    if (persistentStoreCoordinator != nil) {
        return persistentStoreCoordinator;
    }

    NSURL *storeUrl = [NSURL fileURLWithPath: [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"FC.sqlite"]];

    NSError *error = nil;
    persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] …
Run Code Online (Sandbox Code Playgroud)

iphone data-migration core-data

50
推荐指数
2
解决办法
2万
查看次数