views:

487

answers:

2

Hi, I am a software engineer concentrating on iphone application development and I own a developer license. Currently I am working on a project where i need to maintain a local database in iphone. Each time the application launches , the application should sync data from a remote server. The application should add/update/delete the local table entries corresponding to the data from remote server.

I plan to use SQLite DB. I would like to know whether there is any framework available for synchronizing these two databases? Also is there any other DB which is supported by iphone?

A: 

I've recently finished a similar app (at least in terms of database functionality), and I've found Core Data to be invaluable. It lets you define a data model - similar to a database schema from your remote database - then you can pull down data in whatever format is most suited to you and store it in Core Data.

The framework will abstract away things like the underlying persistence mechanism, so you can use SQLite (the default) or a couple other data storage formats (XML is an option, IIRC). You can also manipulate the data records in an object-oriented manner, which is incredibly convenient when driving some other parts of your app (like the UI). Take a look at the Core Data Programming Guide for more info.

As for the synchronization framework itself, I'm not aware of anything prebuilt that does what you're looking for. My solution was to let the SQL daemon on the server generate XML (MySQL happens to have a handy command-line option --xml for just this purpose), then use NSXMLParser on the iPhone end to parse the XML back into Core Data objects (of course other people will recommend libxml - I just stuck with what was most readily available).

Tim
Just FYI, I'm (almost totally sure) that the iPhone implementation of CoreData only allows storing data in an sqlite database, unlike the desktop versions that have several different backing options.
John Biesnecker
According to http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/CoreData/Articles/cdPersistentStores.html#//apple_ref/doc/uid/TP40002875, on the iPhone you don't get XML (so my one example was wrong :), but you do get an atomic and an in-memory store, as well as the ability to define your own store types. Good call, though, on it being more limited than a full Mac platform.
Tim
+1  A: 

I'm also working on this and am going to use Core Data as well. I'm planning on using the Reachability API to determine if I am able to sync, and then use JSON to transfer the data from the remote server. Core Data is really easy to use and you don't have to build SQL directly at all. It also automatically manages keeping objects in memory for you which will help with memory usage like you have in a constrained environment like the iPhone.

Paul Franceus