CoreData如何检测新版本的应用程序,以便我可以更新模型?

问题描述:

我有一个非常简单的模型,当应用程序第一次启动并填充plist的内容时创建。当下载并启动应用更新时,我需要重新创建模型。我知道你可以版本coredata模型,但我不知道模型本身会改变。我只需要通过现有的对象数组,并从plist添加新的对象。为此,我需要检查应用程序的版本号,并以某种方式检测它与设备上的模型不同。

I have a very simple model that is created when the app is launched for the very first time and populated with the contents of a plist. I will need to re-create the model again when an update to the app is downloaded and launched. I know that you can version coredata models but I don't foresee the model itself is going to change. I will just need to go through the existing array of objects and add new objects from the plist. For this I need to check the version number of the app and somehow detect it is different from the model on the device.

看看这个方法在NSPersistentStore

Take a look at this method on NSPersistentStore

+ (BOOL)setMetadata:(NSDictionary *)metadata forPersistentStoreWithURL:(NSURL *)url error:(NSError **)error

您可以设置键/值对的任意字典。使一个代表你的版本。然后你可以加载该字典:

You can set an arbitrary dictionary of key/value pairs. Make one to represent your version. Then you can load back that dictionary with:

+ (NSDictionary *)metadataForPersistentStoreWithURL:(NSURL *)url error:(NSError **)error