开发者

Core data: how to migrate core data without erasing user's data

开发者 https://www.devze.com 2023-02-14 21:48 出处:网络
I have an ipho开发者_JS百科ne app with core data. I pre-populate an entity called DefaultValues before user download the app from app store, he then add his own data to other entities. DefaultValues n

I have an ipho开发者_JS百科ne app with core data. I pre-populate an entity called DefaultValues before user download the app from app store, he then add his own data to other entities. DefaultValues never gets changed. Now, i want to modify some data from my DefaultValues entity and add some new data to it (no change in entity schema). How do i do it??

Off course, i did some research online before asking this question. I found some links here Link 1:

Link 2

but there is no concrete answer to this question.

Should i have to perform core data versioning and data migration?? Or how should i just change DefaultValues entity without erasing users data???

Thanks


As for the actual migration, search for "Lightweight migration". There are some Apple docs about it, also there's a topic here

However, you say you use Core Data to store Default Values. You probably should not use Core Data but instead NSUserDefaults.

Use this bit of code to set a default value for a setting:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setBool:YES forKey:@"SomeSetting"];

you can later get the value for SomeSetting like so:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
BOOL currentSetting = [defautls boolForKey:@"SomeSetting"];

Similarly, you can store strings, numbers and a bunch of other stuff.

0

精彩评论

暂无评论...
验证码 换一张
取 消