What would be the best way to update ReadOnly database with Core Data (iOS)

I am creating an iOS application based on open government data,

I am using CoreData for data storage. There are about 8 objects in my xcdatamodel,

some entity matches 20,000+ rows of data and this database is READ-ONLY,

I will not and the user will not modify a database like DELETE / UPDATE / CREATE, this is a READ-ONLY database.

Open data from government will contain an update, but it is not planned (and not often) and I need to allow the user to update the database if the updated data has been updated.

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

1) Fetch JSON from my server, remove all content from the database and paste again.

Disadvantages: a) The risk is high, the user / system may terminate the application during the update. b) high cost of network traffic

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

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

I think the second one will be better, but I don't know if it will risk it? or are there any disadvantages to this method?

Thank!

+3


source to share


2 answers


If the database is too heavy and read-only, I suggest you update your database and run it as a new version of your app, so when the user updates the app from the appstore, the previous db woud will be removed and the new one will be installed, I would prefer not to risk fetching background data as it can lead to network / application loss errors and the update will hang in the middle, better be safe and new version with updated db



+3


source


There are several ways to do this.

Probably the fastest and most reliable solution is to download the updated SQLite database file from the server in a background job (see NSURLSession

). You can easily "install" the database file on your device by copying it to the desired location.



This will require you to create this database somewhere outside of your application, meaning you need to have control over the web service. This is the harder part. But you need to know that SQLite databases can be shared across different platforms. Thus, 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 a SQLite database on the server that can be used on the device. Thus, the device needs to send all the necessary information (for example, the "version" of the database schema) to the server in order for the server to create the corresponding database.

+1


source







All Articles