iPhone offline application with synchronization
Asked Answered
D

5

29

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?

Diocesan answered 27/3, 2009 at 2:3 Comment(1)
I second this question, and add that I'd like to hear about strategies for merging out-of-sync data. Contacting a server to sync is a no-brainer. The hard part is figuring out how to combine the server data with the local data.Bussey
J
15

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)

Jurgen answered 27/3, 2009 at 12:34 Comment(1)
In the case of multiple devices, you end up with a master-master replication requirement. Make sure you account for conflicts. Consider using GUIDs to avoid id collisions on new records.Meiosis
C
16

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.

Chickaree answered 13/4, 2009 at 21:3 Comment(0)
J
15

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)

Jurgen answered 27/3, 2009 at 12:34 Comment(1)
In the case of multiple devices, you end up with a master-master replication requirement. Make sure you account for conflicts. Consider using GUIDs to avoid id collisions on new records.Meiosis
S
4

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.

Saturday answered 13/4, 2009 at 2:12 Comment(0)
C
2

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.

Cuneo answered 27/3, 2009 at 2:59 Comment(1)
Please don't over-capitalize "Objective-C".Dues
D
1

Wonder if you have considered using a Sync Framework to manage the synchronization. If that interests you can take a look at the open source project, OpenMobster's Sync service. You can do the following sync operations

  • two-way
  • one-way client
  • one-way device
  • bootup

Besides that, all modifications are automatically tracked and synced with the Cloud. You can have your app offline when network connection is down. It will track any changes and automatically in the background synchronize it with the cloud when the connection returns. It also provides synchronization like iCloud across multiple devices

Also, modifications in the Cloud are synched using Push notifications, so the data is always current even if it is stored locally.

Here is a link to the open source project: http://openmobster.googlecode.com

Here is a link to iPhone App Sync: http://code.google.com/p/openmobster/wiki/iPhoneSyncApp

Desperate answered 18/3, 2012 at 17:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.