问题描述
我们正在使用核心数据用于我们的在线目录和它的工作正常和应用程序在应用商店现在我需要升级核心日期与一些字段和属性。它迁移到新的,但已经存储在应用程序中的旧数据完全消失。我尝试了各种方法使用这个代码保留它。
we are using core data for our online catalog and its working fine and app available in app store now I need to upgrade the core date with some of fields and attributes. It migrated to new one but old data already stored in the app are completely vanished. I tried various ways to retain it using this code
NSString *databaseFilePath = [[self applicationDocumentsDirectory] stringByAppendingPathComponent: @"App_iOS.sqlite"];
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager removeItemAtPath:databaseFilePath error:NULL];
NSURL *storeUrl = [NSURL fileURLWithPath: databaseFilePath];
NSError *error = nil;
if( ![[self persistentStoreCoordinator] addPersistentStoreWithType:NSSQLiteStoreType configuration: nil URL:storeUrl options:nil error:&error])
{
[__managedObjectContext insertObjects];
}
else
{
[__managedObjectContext updatedObjects];
}
我还没有得到解决方案,以保留应用程序中的数据。我在互联网上搜索这个大部分的面孔同样的问题,但我还没有得到好的解决方案
I am not get solution yet to retain the data in app. I searched in internet for this most of the face the same problem but I am not receive the good solution yet
推荐答案
现在发现这很简单 - 一旦你知道在哪里看看#
在我的AppDelegate我设置NSPersistentStoreCoordinator - 你需要添加一些选项来告诉它来处理自动迁移: / p>
I've now found out that this is quite simple - once you know where to look.#In my AppDelegate I set-up the NSPersistentStoreCoordinator - and you need to add some options to this to tell it to handle auto-migrate:
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
NSError *error;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]) {
// Handle error
NSLog(@"Problem with PersistentStoreCoordinator: %@",error);
}
然后你需要在xCode中执行:
Then you need to do in xCode:
1. Select your xcdatamodeld file
2. Select the Editor Menu at the top - then choose Add Model Version
3. Now your xcdatamodeld file have two (modelname.xcdatamodel & modelname2.xcdatamodel ) .
4. Now modelname.xcdatamodel have the green check mark implies it is current version, but we need to change the modelname2.xcdatamodel as a current version
5. Select the xcdatamodeld file and then select the View Menu at the top - then Choose Utilities - then Choose the Show File Inspector is shown in right side of Xcode and then Select the Versioned Core Data Model - have Current(DropDownList) - select modelname2(the one you just made current version have green check mark).
6. Now when you install this version onto a device that has the old model - it will automatically upgrade that model to the new model.
这看起来很棒,我想要的简单 - 但我认为你需要在开发过程中小心你改变一个模型 - 否则你将必须为每个更改创建一个新版本。
This seems great and as simple as I wanted - but I think you need to be careful during development as you change a model - otherwise you will have to create a new version for each change.
我想我会做的是,我将保留所有更改的文件和然后一旦我准备好部署我的更新,我将删除所有的中间文件,只是部署与最旧的和最新的型号。
I think what I will do is that I will keep all of the changed files and then once I get ready to deploy my update I'll delete all the in-between files and just deploy with the oldest and latest models.
这篇关于核心数据迁移xcode 4.2不会更新应用程序存储中已有的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!