2011-04-02 116 views
0

我已经在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应用模板和“使用核心数据进行存储”。我必须创建自己的AppDelegate,并且永远不会看到有关PersistentStoreCoordinator的任何内容(但仍然没有,我尝试创建一个新应用程序来检查)。这里有帮助吗?我是Cocoa的新手,但是我的应用程序在没有PersistentStoreCoordinator的情况下工作得很好,这就是为什么我还没有实现一个。我确实有一个我创建的AppDelegate,但是当我将这些代码放在那里时,会引发很多错误。说明:/

EDIT(尼克):这里是新代码: enter image description here

我的头文件: enter image description here

回答

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]; 
} 
+0

@Nick:感谢您的答复。其实我必须创建我自己的AppDelegate,没有包含其中。我将代码的第一部分粘贴到我的实现中,并将代码苹果提供在括号中。运行时,它会在整个地方抛出错误。我究竟做错了什么? – Zakman411 2011-04-02 21:54:13

+0

@Nick:我编辑了我的帖子以显示屏幕截图。感谢您的帮助到目前为止 - 任何更多的帮助将非常赞赏 – Zakman411 2011-04-02 22:00:37

+0

请创建一个干净的模板应用程序,它使用核心数据,然后看应用程序委托。如何将核心数据插入到应用程序中非常简单。将相关方法移到您的应用程序中。您提供的代码不会编译,因为您拥有占位符,例如'<#Store type#>',它必须用实际代码替代。你可以用淡蓝色的背景来识别它们。 – 2011-04-02 22:53:58