开发者

Core Data information in application

开发者 https://www.devze.com 2023-04-09 10:33 出处:网络
I have an app that I\'m building where it has some data built into it that is contained in a core data data model.When I run the app to the simulator, the data is there and everything is peachy.

I have an app that I'm building where it has some data built into it that is contained in a core data data model. When I run the app to the simulator, the data is there and everything is peachy. However, when I deploy the app to the device (an iPad), the app works fine, except there is no data. Seems like a no brainer, but can't seem to find a switch or check box or anything to tell xcode to deploy the data along with the model.

Update:

The suggested duplicate didn't really help me. I'm not sure I entirely understand what needs to be done. I have the sql开发者_StackOverflow社区lite db with my initial set of data, do I need to load that into core data every time I launch the app?

Also, what about data that the users edits/adds/deletes?

Does that then get lost or is that allowed to stay in core data?


What you need to do is prepare an initial set of data as per the duplicate question above.

The database file will be bundled with your app. That location - in the main bundle - will be different to the deployed location. In projects I've worked in, we use the application documents directory to be the deployed location.

So, the logic goes as follows. In your app delegate, in the didFinishLaunchingWithOptions method, check to see if the .sqlite file exists in your application documents directory. If it doesn't get the file from your main bundle and copy it to that location.

From that point on, use that file for all your database operations.

That's how you include a preloaded database in your app.

Now, what about creating the preloaded database in the first place? For that, you should create another target in your project. That target will create the database based upon the data model, and populate it with your initial data. Once it's created, you should drag and drop that file back onto Xcode so that it's included in your app's resources bundle.

0

精彩评论

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

关注公众号