iPhone核心数据“自动轻量级迁移” [英] iPhone Core Data "Automatic Lightweight Migration"

查看:141
本文介绍了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(@未解析的错误%@,%@,错误,[错误userInfo]);
abort();
}

return persistentStoreCoordinator;
}

这是从以下URL:
Doc a>



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


-12-01 20:04:22.877



Shoppee [25633:207]错误:错误



Domain = NSCocoaErrorDomain Code = 134130



UserInfo = 0x1624d20无法完成操作(可可错误
134130.)2009-12-01 20:04:22.879 Shoppee [25633:207]未解决的错误
错误域= NSCocoaErrorDomain代码= 134130 UserInfo = 0x1624d20
无法完成操作(Cocoa错误134130.),{
URL = //localhost/Users/Eric/Library/Application%20Support/iPhone%20Simulator/User/Applications/A8A8FB73-9AB9-4EB7-8F83-82F5B4467AF1/Documents/MyApp.sqlite;
metadata = {
NSPersistenceFrameworkVersion = 241;
NSStoreModelVersionHashes = {
Item =< 869d4b20 088e5c44 5c345006 87d245cd 67ab9bc4 14cadf45
180251e9 f741a98f>;
Store =< 47c250f4 895e6fd1 5033ab42 22d2d493 7819ba75 3c0acffc
2dc54515 8deeed7a>;
};
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers =(
);
NSStoreType = SQLite;
NSStoreUUID =8DC65301-3BC5-42BE-80B8-E44577B8F8E1;
};
reason =无法找到源存储模型; }


看起来我需要包含原始数据模型,但我不知道该怎么做。任何建议?

解决方案

摘要/完整指南:




  1. 在进行任何更改之前,请创建新的模型版本。



    在Xcode 4中:选择 .xcdatamodel - >编辑器 - >添加模型版本。



    在Xcode 3:Design - >数据模型 - >添加模型版本。



    您会看到在 <$ c $>中创建了一个新的 .xcdatamodel


  2. 保存文件夹


  3. 选择新的 .xcdatamodel ,并根据 轻量级迁移文档


  4. 保存。


  5. 将当前/活动模式设置为新创建的模式。



    选择 .xcdatamodeld 文件夹:



    在Xcode 4:实用工具侧栏 - >文件检查器 - >版本化核心数据模型 - >选择新模式。



    在Xcode 3:设计>数据模型>设置当前版本。



    .xcdatamodel 图示上的绿色勾号会移至新架构。


  6. 实施必要的代码在运行时执行迁移。



    创建 NSPersistentStoreCoordinator (通常是AppDelegate类), code>参数,用以下代码替换 nil

      [NSDictionary dictionaryWithObjectsAndKeys:
    [NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,
    [NSNumber numberWithBool:YES],NSInferMappingModelAutomaticallyOption,nil]


  7. 运行您的应用程序。如果没有崩溃,您可能已成功迁移:)


  8. 成功迁移后,可以删除迁移代码(步骤7)。 (由开发人员决定何时可以将已发布应用的用户视为已迁移。)


重要:不要删除旧的模型版本/模式。 Core Data需要旧版本才能迁移到新版本。


I am attempting to update an app that implements a core data store. I am adding an attribute to one of the entities.

I added the following code to my delegate class:

- (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;
}

This was from the following URL: Doc

I get the following error when executing the code:

2009-12-01 20:04:22.877

Shoppee[25633:207] Error: Error

Domain=NSCocoaErrorDomain Code=134130

UserInfo=0x1624d20 "Operation could not be completed. (Cocoa error 134130.)" 2009-12-01 20:04:22.879 Shoppee[25633:207] Unresolved error Error Domain=NSCocoaErrorDomain Code=134130 UserInfo=0x1624d20 "Operation could not be completed. (Cocoa error 134130.)", { URL = file://localhost/Users/Eric/Library/Application%20Support/iPhone%20Simulator/User/Applications/A8A8FB73-9AB9-4EB7-8F83-82F5B4467AF1/Documents/MyApp.sqlite; metadata = { NSPersistenceFrameworkVersion = 241; NSStoreModelVersionHashes = { Item = <869d4b20 088e5c44 5c345006 87d245cd 67ab9bc4 14cadf45 180251e9 f741a98f>; Store = <47c250f4 895e6fd1 5033ab42 22d2d493 7819ba75 3c0acffc 2dc54515 8deeed7a>; }; NSStoreModelVersionHashesVersion = 3; NSStoreModelVersionIdentifiers = ( ); NSStoreType = SQLite; NSStoreUUID = "8DC65301-3BC5-42BE-80B8-E44577B8F8E1"; }; reason = "Can't find model for source store"; }

It looks like I somehow need to include the original data model but I am not sure how to do that. Any suggestions?

解决方案

To recap/Full guide:

  1. Before making any change, create a new model version.

    In Xcode 4: Select your .xcdatamodel -> Editor -> Add Model Version.

    In Xcode 3: Design -> Data Model -> Add Model Version.

    You will see that a new .xcdatamodel is created in your .xcdatamodeld folder (which is also created if you have none).

  2. Save.

  3. Select your new .xcdatamodel and make the change you wish to employ in accordance with the Lightweight Migration documentation.

  4. Save.

  5. Set the current/active schema to the newly created schema.

    With the .xcdatamodeld folder selected:

    In Xcode 4: Utilities sidebar -> File Inspector -> Versioned Core Data Model -> Select the new schema.

    In Xcode 3: Design > Data Model > Set Current Version.

    The green tick on the .xcdatamodel icon will move to the new schema.

  6. Save.

  7. Implement the necessary code to perform migration at runtime.

    Where your NSPersistentStoreCoordinator is created (usually AppDelegate class), for the options parameter, replace nil with the following code:

    [NSDictionary dictionaryWithObjectsAndKeys:
                      [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption, 
                      [NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil]
    

  8. Run your app. If there's no crash, you've probably successfully migrated :)

  9. When you have successfully migrated, the migration code (step 7) can be removed. (It is up to the developer to determine when the users of a published app can be deemed to have migrated.)

IMPORTANT: Do not delete old model versions/schemas. Core Data needs the old version to migrate to the new version.

这篇关于iPhone核心数据“自动轻量级迁移”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆