CoreData:Migrate data from bundled db

前端 未结 2 2002
心在旅途
心在旅途 2020-12-20 06:45

Our app is having a coredata store which is based on a single coredata model. There are read-only data as well as read-write data.

The read-only data are pre-loaded

相关标签:
2条回答
  • 2020-12-20 07:05

    If I have understood your question: you want to update read-only data during app update--while leaving read-write data that user has changed intact.

    There are several ways to achieve this:

    1. Have two separate databases. One database can have the read-write data and another with read-only data. They can relate to each other using fetched properties. During update, replace or update the read-only database--while leaving the read-write one intact.
    2. Update the database using a background thread. The update code will have it's own ManagedObjectContext--but sharing the same PersistentStore. Synch the main ManagedObjectContext from the background thread using some protocol.

    The second option of updating from a background thread might work well if you choose to update from your web service.

    If I haven't understood your issue, please clarify.

    0 讨论(0)
  • 2020-12-20 07:06

    Ok So finally after a lot of research I achieved my goal, below are the trails and solutions that i did

    Sol 1

    1. Have the read-only and read-write data in separate databases, so that I can safely delete the readonly db if there is any master data update and I can safeguard user's data, but considering the timelines and constraints that I have, it wont be possible for me. Posting here so that it might help others.

    Sol 2

    I thought rather trying to merge the new data from bundled DB to the existing DB, I thought of merging the user data from existing db to the new db. Below are the steps done. --> Created a new datacontext.

    --> Created a new persistent co-ordinator

    --> Renamed the bundled db with _v2 and copied it to the Doc directory, now we have 2 DB in the doc dir I took some app Importing large data sets

    --> Now using the ManagedObject clone category, I copied all the user info data from the existing db to the new db _v2. Found the category here NSManagedObject+Clone

    --> Worked fine, now I got my _v2 database with new readonly data and the user data from the old database.

    --> Now I need to give control back to the default datacontext

    --> I tried to change the PSC of the old context to the new PSC but system didnt allow me to do that.

    --> I then tried to change the persistence store of the old context to the new store but I got error saying that database already exists. (migratePersistentStore:toURL:options:withType:error:)

    --> I ran out of ideas here.

    Sol 3

    I then discussed my problems with some of my other colleagues and they suggested to provide the new data in a different format and that striked. As I already mentioned, my app has logic to download new data as JSON and merge it to core data, why can I provide a JSON file with the new data, along with my app?

    I collected the new response from thew webservice and created a JSON (not big just 1.5MB) and attached with app bundle, and for users that update the app, instead of core data merging I will read the JSON data locally and do the initial merging to the core data DB, there by the data base will have the new readonly data and also user data intact. After the initial merge, everything will be taken care by online sync.

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