开发者

Best way to initialize core data for an iPhone app?

I have a rather long method that initializes my core data.

I can think of two ways to implement this in the final app:

  • include the method in the app and execute it if there's no data.

  • exclude the method from the app,开发者_开发技巧 create an SQLite database in advance, include it in the bundle and copy it to the documents folder if there's no data.

Which one is better, or should I just toss a coin? Is one of them safer for future OS updates?


The future updates to the data model will be easy with auto migration no matter which way you choose, but future defaults will be more difficult to handle if you choose the bundled DB. For instance, your long time users won't get any future defaults you add, so if you put a great new feature in, and they already have a DB, they won't get the defaults for that feature unless you put them in code, at which point you have to write code anyway. Thus, I would lean towards a method based solution.

  1. Create a NSUserDefaults key that has a "CurrentDataVersion" key
  2. When the app is run, read this key, compare it against a hard-coded version that represents what should be current, then use it to determine which data should be added.
  3. After adding the new data, update the CurrentDataVersion key so it doesn't run again

This solves the issue of older customers getting newer defaults and is more user friendly in terms of future updates. Because you mentioned your method is long, a standard way of presenting this to the customer is to put up a spinner and something along the likes of "Initializing… This will only happen once"

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜