Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS App Offline and synchronization

I am trying to build an offline synchronization capability into my iOS App and would like to get some feedback/advice from the community on the strategy and best practice to be followed to do the same. The app details are as follows:

The app shows a digital catalog to users and allows them to perform actions like creating and placing orders, among others. Currently the app only works when online, and we have APIs for all actions like viewing the catalog, creating/placing orders which return JSON data. We would like to provide offline/synchronization capability to users, through which users can view the catalog and create/place orders while offline, and when they come online the order details will be synchronized and updated to our server. We would also like to pull the latest data from the server, and have the app keep itself up to date in case of catalog changes or order changes that happened at the Server while the app was offline. Can you guys help me to come with the best design and approach for handling this kind of functionality?

like image 722
wquamar Avatar asked Jul 26 '13 19:07

wquamar


2 Answers

I have done something similar just in the beginning of this year. After I read about NSOperationQueue and NSOperation I did a straight forward approach:

Whenever an object is changed/added/... in my local database, I add a new "sync"-operation to the queue and I do not care about, if the app is online or offline (I added a reachability observer which either suspended the queue or takes it back working; of course, I do re-queueing if an error occurs (lost network during sync)). The operation itself reads/writes the database and does the networking stuff. My ViewController use a NSFetchedResultsController (with delegate=self) to get callbacks on changes. In some cases I needed some extra local data (it is about counting objects), where I have used NSManagedObjectContextObjectsDidChangeNotification.

Furthermore, I have used Multi-Context CoreData which sounded quite reasonable to use (I have only two contexts).

To get notified about changes from your server, I believe that iOS 7 has something new for you.

On the server side, you should read a little for the actual approach you want to go for: i.e. Data Synchronization by Dan Grover or Developing Android REST Client Applications (of course there are many more good articles out there).

like image 180
theguy Avatar answered Oct 07 '22 18:10

theguy


Caution: you might be disappointed when you expect an easy solution. Your requirement is not unusual, but the solution might become more complex than you expect - depending on the "business rules" and other reasonable requirements. If you intelligently restrict your requirements you may find a solution which you can implement yourself, otherwise you may also consider to use a commercial product.

I could imagine, that if you design the business logic such that it takes an offline state into account and exposes this explicitly in the business logic, you may find a solution which you can implement yourself with moderate effort. What I mean by this is for example, when a user creates an order, it is initially in "not committed" stated. The order will only be committed when there is access to the server and if the server gives the "OK" that this order can actually be placed by this user. The server may also deny the order, sending corresponding messages to the user.

There are probably quite a few subtle issues that may arise due to the requirement of eventual consistency.

See also this question which contains pointers to solutions from commercial products, and if you visit their web sites give valuable information about the complexity of the problem and how this can be solved.

like image 28
CouchDeveloper Avatar answered Oct 07 '22 17:10

CouchDeveloper