我已经在App Store上创建并分发给许多客户的Mac App,我需要在Core Data Model中添加一个属性。我阅读了Apple提供的文档,可用here。
但是,它提供了此代码块以启用自动迁移:
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 Template,并启用了“使用核心数据存储”。我不得不创建自己的AppDelegate,从来没有看到任何关于PersistentStoreCoordinator的东西(但仍然没有。我试过创建一个新的应用程序只是为了检查)。这里有什么帮助?我是Cocoa的新手,但我的应用程序完全正常,没有PersistentStoreCoordinator,这就是为什么我还没有实现一个..我确实有一个我创建的AppDelegate,但是当我把这个代码放在那里时会引发很多错误。帮助:/
EDIT(for nick):这是新代码:
我的头文件:
答案 0 :(得分:1)
在这里:核心数据相关的标题和方法。
应用程序代理标题 - 核心数据相关部分
@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];
}