在哪里放置代码以配置永久存储以进行迁移 [英] Where to Put Code Configuring Persistent Store for Migration
问题描述
我已经创建了Mac App,并在App Store上将其分发给许多客户,并且需要向Core Data Model添加属性。我阅读了Apple提供的文档,可在此处获得。
I have a Mac App already created and distributed on the App Store to many customers, and I need to add an attribute to the Core Data Model. I read the documentation provided by Apple, available here.
但是,它提供了以下代码块来启用自动迁移:
However, it gives this block of code to enable automatic migration:
NSError *error;
NSPersistentStoreCoordinator *psc = <#The coordinator#>;
NSURL *storeURL = <#The URL of a persistent store#>;
NSDictionary *optionsDictionary =
[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
forKey:NSMigratePersistentStoresAutomaticallyOption];
NSPersistentStore *store = [psc addPersistentStoreWithType:<#Store type#>
configuration:<#Configuration or nil#>
URL:storeURL
options:optionsDictionary
error:&error];
。而且我不知道该放在哪里。有人提到(在另一个线程中),它进入了PersistentStoreCoordinator,但是,我只是使用了默认的Cocoa App模板并启用了使用核心数据进行存储。我必须创建自己的AppDelegate,却从未见过有关PersistentStoreCoordinator的任何信息(现在仍然没有。我试图创建一个新应用来进行检查)。这里有什么帮助吗?我是Cocoa的新手,但是我的应用程序在没有PersistentStoreCoordinator的情况下运行良好,这就是为什么我还没有实现。.我确实创建了一个AppDelegate,但是当我在其中放置此代码时,它会引发很多错误。帮助:/
.. And I have no idea where to put that. Someone mentioned (in another thread) that it goes into a PersistentStoreCoordinator, however, I simply used the default Cocoa App Template with "Use Core Data for Storage" enabled. I had to create my own AppDelegate and never saw anything about a PersistentStoreCoordinator (and still don't. I've tried creating a new app just to check). Any help here? I'm new to Cocoa but my app works perfectly fine without a PersistentStoreCoordinator, which is why I haven't implemented one yet.. I do have an AppDelegate I created, but when I put this code in there it throws many errors. HELP :/
编辑(针对昵称):这是新代码:
EDIT (for nick): Here is the new code:
我的头文件:
And my header file:
推荐答案
在这里,您可以找到:核心数据相关标头和方法。
Here you go: Core Data relevant header and methods.
应用程序委托标头-核心数据相关部分
@interface CoreDataFirstStepsAppDelegate : NSObject <UIApplicationDelegate>
{
// ...
@private
NSManagedObjectContext *managedObjectContext_;
NSManagedObjectModel *managedObjectModel_;
NSPersistentStoreCoordinator *persistentStoreCoordinator_;
}
// ...
@property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
@property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
@property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
应用程序委托实现-核心数据相关部分
/**
Returns the managed object context for the application.
If the context doesn't already exist, it is created and bound to the persistent store coordinator for the application.
*/
- (NSManagedObjectContext *)managedObjectContext {
if (managedObjectContext_ != nil) {
return managedObjectContext_;
}
NSPersistentStoreCoordinator *coordinator = [self persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext_ = [[NSManagedObjectContext alloc] init];
[managedObjectContext_ setPersistentStoreCoordinator:coordinator];
}
return managedObjectContext_;
}
/**
Returns the managed object model for the application.
If the model doesn't already exist, it is created from the application's model.
*/
- (NSManagedObjectModel *)managedObjectModel {
if (managedObjectModel_ != nil) {
return managedObjectModel_;
}
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"CoreDataFirstSteps" ofType:@"momd"];
NSURL *modelURL = [NSURL fileURLWithPath:modelPath];
managedObjectModel_ = [[NSManagedObjectModel alloc] initWithContentsOfURL:modelURL];
return managedObjectModel_;
}
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
if (persistentStoreCoordinator_ != nil) {
return persistentStoreCoordinator_;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"CoreDataFirstSteps.sqlite"];
NSError *error = nil;
persistentStoreCoordinator_ = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![persistentStoreCoordinator_ addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error]) {
NSError *error;
NSURL *storeURL = storeURL;
NSPersistentStoreCoordinator *psc = persistentStoreCoordinator_;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption, nil];
if (![psc addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:storeURL
options:options
error:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
return persistentStoreCoordinator_;
}
/**
Returns the URL to the application's Documents directory.
*/
- (NSURL *)applicationDocumentsDirectory {
return [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
}
这篇关于在哪里放置代码以配置永久存储以进行迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!