在哪里放置代码 配置持久存储以进行迁移
我已经创建了一个 Mac 应用程序并在 App Store 上分发给许多客户,我需要向核心数据模型添加一个属性。我阅读了 Apple 提供的文档,可在此处获取。
但是,它提供了这段代码来启用自动迁移:
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,但是当我将这段代码放在那里时,它会抛出许多错误。帮助:/
编辑(昵称):这是新代码:
我的头文件:
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];
.. 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:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这里,您可以:核心数据相关标头和方法。
应用程序委托标头 - 核心数据相关部分
应用程序委托实现 - 核心数据相关部分
Here you go: Core Data relevant header and methods.
Application Delegate Header - Core Data relevant parts
Application Delegate Implementation - Core Data relevant parts