Saturday, March 24, 2012

Phonegap-SQLitePlugin: reliable offline storage for iOS

UPDATE: Please see my new posting: Cordova/PhoneGap sqlite plugins offer large db size, excellent reliability

There has been a bit of discussion on the Cordova/PhoneGap forum that "LocalStorage or SQLite data will no longer be backup and can be deleted" for HTML5 as of iOS 5.1. This news has given me a very bad feeling while I am developing my first iPhone app and I was immediately searching for a more reliable solution. I tried using the Application Preferences plugin but it did not work very well either on the iPhone simulator or on my iPhone device. I discovered the Phonegap-SQLitePlugin which does seem to provide a much more reliable solution.

There has been quite a bit of effort to solve this problem in the Corvdova/PhoneGap project. Kerri Shotts had been kind enough to contribute a workaround that does the backup and restore tasks during startup, "resign active", and termination phases and I did consider using this workaround very seriously. I chose not to use this workaround because first I would have to do separate testing to verify that this is working both pre-iOS 5.1 and iOS 5.1+ and second I have some doubts about its reliability. I am very concerned that if an app or even an iOS device crashes then some user data could be lost. Apache issue CB-330 had been logged and some work had been done in the incubator-cordova-ios project which still uses the backup-and-restore mechanism.

In general, I like the Phonegap-SQLitePlugin much better due to its reliability. Currently this SQLite Plugin works on PhoneGap 1.3 and earlier but not on Cordova 1.5 due to the changes in the iOS plugin API. The SQLite Plugin exposes an API that is similar but not the same as the HTML5 Web SQL API. In general I have been able to find plenty of answers on the Internet about how to format SQLite queries and the PhoneGap SQLite Plugin API should follow the SQLite C API very closely.

In general I am very happy to use the Phonegap-SQLitePlugin. There are a few issues that I would like to see solved in the near future. Cordova support is currently missing and I have had no time to try to make this work. In addition, it would be nice to see an adaptation or even a wrapper to emulate the HTML5 Web SQL API.