简体   繁体   中英

One way sync website > iPhone app using Core Data

I'm building an iPhone application with a database (most likely I'll use Core Data for the first time, but it can still turn out that I'll be using SQLite depending on your answers here).

The content of this database should be updated on a regular base from some online storage (most likely a MySQL database).

As the initial database will be rather large it will be in the app so the user won't need to download that, the updates will be rather small so those can be downloaded. You can see that 'Delta' updates are a must (downloading the whole dataset each time is just too much data). Updates will consist of a few new records and eventually some corrections of existing records

The local database will have a few tables/entities (with a lot of relations between them) that need to be updated, those tables/entities will be read-only (*). There will be another few tables/entities where the user can enter comments (read/write) but they never have to get updated, this is private user data. As a matter of fact I'll sometimes create an extra table with just a 1-1 relation to keeps nicely separated (dataset tables/entities versus comments tables/entities).

Can somebody provide me with a way to generate a file online with all the changes in the db, that can be downloaded to the phone and efficiently and correctly parsed so that those changes/new records are applied. I've been thinking like something similar to the migrations Rails uses to maintain its database, but can't seem to find anything that looks clean and performant. Keeping in mind dat relations between different tables/entities is very important, that's were my inspiration is failing...

(*) for a start read only, later on the user will be able to add records and edit them. He will never be able to edit the existing records, and his own records don't need to go back to the server.

In a broad, architectural sense, here is what I would suggest:

  1. Take a look at Restkit which provides an excellent iOS library for accessing Web API and super simple means of storing fetched information to a local CoreData instance
  2. Implement a RESTful JSON interface to surface the data from your MySQL database to a JSON representation. This should be straightforward with any modern web framework. Django provides Piston, Rails does this out of the box, Drupal has the Services framework and Symfony has similar functionality.
  3. Store your "Last Updated" status in an NSUserDefaults variable and then append that datetime to each request you make to your webservice. This will allow you to filter down your resultset to deltas

I can't recommend RestKit highly enough. Stick out a few hours of fighting the learning curve and you'll never look back.

As an alternative i would suggest AFNetworking+MagicalRecord because you mention Rails in your question. (Restkit was for me too complicated)

  1. AFNetworking will handle communications with server
  2. AFOAuth1Client ** AFOAuth2Client ** will handle authorization (with Doorkeeper if you use rails for example)
  3. MagicalRecord will handle import and querying your core data in ActiveRecord style.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM