How can I ship my app with a pre-populated Core Data database?

后端 未结 5 1398
天涯浪人
天涯浪人 2021-02-01 08:16

My app uses Core Data and I want some default entries to be inside. What\'s best practices of how to do that?

相关标签:
5条回答
  • 2021-02-01 08:25

    If you're already loading the pre-load data via a temporary routine for testing in your current code there's no reason you can't use the sqlite file it creates in the simulator's directory (no need to write a separate Mac app).

    If you're not already filling that db you can still write an iOS app that does it. Odds are you've already written the methods for adding data to your store so you can use them to import the pre-load data as well.

    Either way you'd grab the sqlite file from the simulator's directory and add it to your app's bundle; on first launch you'll copy it into the appropriate place in the app's directory before pointing Core Data to it. If it's really large the downside is that there will be a copy in the bundle and another on disk, but there's not much you can do about that other than grabbing the data over the network.

    As others have suggested, if the amount of data is small you can just import it at first launch, using the methods you've already written for adding data as part of the normal app's workflow.

    0 讨论(0)
  • 2021-02-01 08:28

    There is Core Data Editor at the app store. Alternatively you could build your own simple mac app just for this particular DB and manage it from there. If the amount of default entries is small, then you're better off storing it in a plist or something and loading it into DB after the first launch.

    0 讨论(0)
  • 2021-02-01 08:42

    I would just create a database and put add it to my target so that Xcode copies it into the app bundle. At the first launch just copy it from the app bundle to eg. the documents directory or wherever your app expects the database.

    0 讨论(0)
  • 2021-02-01 08:43

    In iOS 5, my app was rejected if I put a database file into resource bundle. So, I have to download the database from internet instead.

    0 讨论(0)
  • 2021-02-01 08:49

    See the CoreDataBooks example, which has sample code for copying a database at first launch.

    EDIT: I've created a Core Data framework (read about it here: http://bikepress.org/?p=1120) that includes this feature.

    0 讨论(0)
提交回复
热议问题