Johnny
Johnny

Reputation: 2839

Which will be the best way to update users ReadOnly database using Core Data (iOS)

I'm building an iOS app based on our goverment's open data,

I use CoreData to store the data, There are about 8 entities in my xcdatamodel,

some entity cotains 20000+ row of data's, And this database is READ-ONLY,

I won't and user won't modify the database, such as DELETE/UPDATE/CREATE, this is a READ-ONLY database.

The open data from the goverment will keep update, but it isn't scheduled (and not often), and I need to let user update the database if the open data was updated.

I am considering which one is the best way to update users database?

1) Fetch JSON from my server, delete all contents from the database, and insert again.

Disadvantages: a) The risk is high, user/system may terminate the app while is updating. b) lots of cost of network traffic

2) Clear the database, Fetch all entities data from my server on the iOS simulator, insert the data,

After that, copy the database from the Documents folder, put it on server, let my user download the whole sqlite database, and cover the old database.

I think the second one will be better, But I don't know will this take any risk? or there are disadvantages of this method?

Thank you!

Upvotes: 3

Views: 131

Answers (2)

CouchDeveloper
CouchDeveloper

Reputation: 19154

There are several ways to accomplish this.

The likely fastest and most reliable solution is to download the up to date SQLite database file from the server in a background task (see NSURLSession). You can easily "install" the database file on the device through copying to the appropriate location.

This will require that you create this database somewhere outside of your app - that is, you need to have control about the webservice. This is the more tricky part. But you need to know that SQLite databases can be shared across platforms. So, it is possible to create a SQLite database on a server whose structure (defined by the database schema) matches that on the device. In any case, you need to know exactly how to create the SQLite database on the server which can be used on the device. So, the device needs to send all required information (e.g. the "version" of the database schema) to the server, so that the server can create the appropriate database.

Upvotes: 1

Geet
Geet

Reputation: 2437

If the database is too heavy , and read-only, then I would suggest you upgrade your database and launch it as a new version of your application, so when the user updates the app from the appstore, the previous db woud be deleted and the new one will be installed, I would rather not take any risk of doing background data fetch as, it may lead to network lost/ app termination errors, and the updation would be stuck in the middle, better be safe and relase a new version with the updated db

Upvotes: 3

Related Questions