Choosing the Right Long-Term Storage Model for Data
The long-term storage model for your data describes where information goes when your application goes away. Even mobile devices that are on all the time and keep applications running in the background will need a safe and structured place to store data that has a long lifetime. A common example of this on mobile phones is the PIM (Personal Information Manager). A user who places his address book onto a phone expects that data to be there regardless of what states the phone goes through. If at all possible, this data should be automatically backed up to a server or desktop computer to facilitate quickly restoring it if the device's long-term storage fails. There are two fundamental questions that you need to answer with regard to your mobile device application's long-term storage model: Should the data be stored in a file or in a database?
If you only have a small amount of data to store, a text file is probably your best bet. If your stored file needs maximum portability between devices, XML is a great choice. On the other hand, if your application is working with a large amount of data that needs to be randomly accessed, if the data needs to be secured, or if you want to be able to use rich querying, transaction, or synchronization abilities, a database is the best choice. The main downsides of using a database are the additional size and processing overhead required along with any additional setup steps that may be required to deploy your application. If a database is used, should it be on or off the device?
Just as with desktop applications, it is possible to directly access databases running on servers or to host databases locally. The advantage of an on-device database is availability; it will be there whether or not you have a connection to a server. If a large amount of data needs to be accessed, a local database may be much more efficient than pushing query results down a comparatively slow wireless connection. The downside of a local database is the additional application deployment complexity and on-device footprint required to host the database. It is also important to know that different mobile devices have different levels of support for hosting local databases. For example, as of this writing, the SQL CE database was supported on Pocket PCs but not Smartphones. If your application is unable to have a database on the local device, you will most likely need to devise a custom mechanism for caching the most important, frequently accessed, or expensive-to-obtain data on the local device.
Flash or RAM File Storage for Your Data?
Mobile devices usually employ one of two strategies for long-term information storage. Data is either stored into flash memory or it is stored in a RAM file system. Flash memory is memory that does not require constant power to keep its contents stable. Flash memory can either come in the form of a removable card (for example, Secure Digital, Compact Flash) or be permanently built in to the device. Today flash memory is available in large-capacity formats; 512MB cards are not uncommon now, and the numbers are going up. Flash memory is also less power consumptive than RAM because it does not need to be electrically refreshed continually. However, flash memory is slower to access than volatile RAM; this is particularly true for writing data. Also any storage bits in flash memory can usually only be updated a limited number of times before they wear out; this number is typically above several hundred thousand writes, which is large but not endless. Although great advances have been made in flash memory to increase its flexibility, it is still not as flexible as RAM. Flash memory is presently suitable to use as a disk replacement but not as a replacement for volatile program RAM. It is also worth noting that SIM cards in GSM mobile phones contain flash memory to hold a limited amount of data as well. This data space was traditionally used for storing the mobile phone's address book, but on newer phones most of this data is stored in the phone's storage for quicker and more flexible access. SIM card storage is still useful for storing specific "secrets" such as cryptographic certificates and keys, but is used less and less for general storage needs due to the availability of other general-purpose flash memory. Think of flash memory as conceptually similar to a hard drive but with no moving parts. The USB storage keys you can stick into your PC are also flash memory. As with hard drives, flash memory usually has a file system built on top of it to allow it to be easily accessed in a structured way. The other common method for persisting information long-term on devices is by using a RAM-based file system. In contrast to flash file systems, RAM file systems use a portion of a device's available memory to hold a file system there. RAM-based storage results in fast file access for both reading and writing but consumes significantly more power than flash. RAM file systems require some power even when the devices are turned off; should the device's battery be removed or run out of energy the file system's contents will be lost. This makes RAM file systems more susceptible to loss than flash systems. On many devices, including Windows CE / Pocket PCbased devices, files are typically stored in RAM file systems using compression algorithms. This provides greater capacity but incurs a processing penalty because data streams must be compressed during reads and writes. RAM file systems are fast, typically significantly faster than flash storage, but not as fast as in-memory application data. Devices can support both flash and RAM file systems simultaneously the same way a desktop can support multiple hard drives. Even closely related devices can differ significantly in their long-term storage models. For example, the Pocket PC and Smartphones differ in the model they use for long-term data storage. By default, Pocket PCs offer a RAM file system; this is a reason why Pocket PC devices have bigger batteries than Smartphones. Smartphones primarily use flash for file system storage. Both Pocket PC and Smartphone devices support having flash memory cards inserted to them dynamically; this allows the devices to access songs, pictures, or any other data that may be on the cards. The capacity and efficiency of file storage on different classes of devices is yet another reason why "write once, run anywhere" strategies for mobile device applications tend to produce unsatisfactory results. How well your mobile application can move data into and out of memory is dependent on the storage medium being used and the capacity present on the device. It is important to know what storage formats target devices support and to consider how your application will utilize these facilities. Knowing this will enable you to optimize your on-device data access and storage strategy based on your application's performance and reliability needs. An explicit understanding of long-term storage mechanisms will also aid you in efficiently porting a mobile application from one device class to another. |
|