views:

2149

answers:

5

I'm looking into building an application which works just as well offline as it does online. Since the application cannot communicate with the server while in offline, there is some level of synchronization which needs to take place.

What are some good tools to read about and start thinking about when planning offline operations with synchronization for your iPhone?

What tools would I have to create on my own, versus tools that apple already provides to help with this particular issue?

+1  A: 

I would store all the information I gather while offline in a SQLite database. Then, on user 's request, you can SYNC all the stored information with a server using HTTP or a custom TCP/IP protocol you can come up with.

I have been using this approach on Palm OS applications for almost 10 years now, and they do work very effectively.

As far as I know, the only "tool" you will have to accomplish this is plain old OBJECTIVE-C with Cocoa Touch. Although you could use some TCP/IP C++ libraries that will make your life easier if you decide to implement your own protocol.

Pablo Santa Cruz
Please don't over-capitalize "Objective-C".
Jonathan Sterling
+8  A: 

there are plenty of application on the app store which rely on both online as well as offline data

what you should really be doing is on start of your app, run a background thread (which runs silently so your user never sees any delay). this thread downloads the latest data from your server and pushes it into your local database (sqlite is the best choice)

make sure you implement some kind of data versioning so that your app only downloads data which is actually changed since last download - else you would unnecessarily be downloading the entire dataset which can be quite huge (depending upon your app requirements)

also make sure to test for internet connectivity when doing this. if no internet is available, alert the user for sure

this way you get the best of both worlds. users when away from internet can still use your app with their local sqlite data

in iphone os 3.0 apple has introduced push services - where you can simply "PUSH" your data instead of doing a "PULL" however this is not available in the current iPhone OS (2.x.x)

Raj
+3  A: 

Push is probably not a viable option here, since the amount of data you can push is miniscule, and basically comes back to "tell my app to make a server call". We use an online/offline model in Satchel. Whenever we have to communicate with the server, we bundle that communication (a URL and possibly some POST data) and store it to a database. If we're online, we pull it right back out, send it, and when we get a valid response back, we remove the record from the database. If we're offline, those rows build up, and the next time we ARE online, they get sent out. This is not a workable model in all situations, but can be adapted to most.

In 3.0, you've got access to CoreData, which is a great data management tool. Other than that, the NSURLXXX family is your friend.

Ben Gottlieb
+2  A: 

http://blog.webscale.co.in/?p=159 This article lists the approaches for handling offline/online data synchronization. It can help you, if you want to create your own framework.

Adi
+5  A: 

Hey,

I've been working on an app that handles this exact behavior for the last 2 months or so. It has a small subset of functions which are online only and a large set of functionality that is offline/online.

I'm using sqlite for local storage as suggested here with a modified version of the sqlitepersistentobjects library. The base version of sqlitepersistentobjects is not thread safe so watch out if you are using it. (check out objectiverecord in: objectivesync for a thread safe alternative but be prepared to dig into the code). If you are willing to develop for the 3.0 sdk then core data is another possibility for a sqlite library.

The overall architecture is simple enough I have modeled local storage using sqlite and remote interaction using objective resource against a rails app and REST api. It can use either xml or json for data serialization.

When an object is modified locally the change is first saved to the sqlite database record for that object and then added to a queue which is serialized and stored in the local sqlite db as well. (The queue can then be processed at any time)

If there is a connection available any queued local changes are deserialized and added to an NSOperationQueue which then processes them in the background.

In order to make this all work I've subclassed NSOperation so that it can support several types of remote queue operations - create, update, delete essentially using objective resource to make the remote requests.

The nice thing about using NSOperationQueue and NSOperation is that they handle the background threading for you so I'd highly recommend having a look at the apple docs for those classes and also at the apple threading guide.

When the application loads there is a bit of remote checking done and processed in the background to pull down the latest data - although to be honest I am still changing the way this behaves a bit.

That's a quick overview of what I've had to deal with so far...hope it helps a little.

paulthenerd