bruce scharlau, university of aberdeen, 2012 data storage options for mobiles mobile computing
TRANSCRIPT
Bruce Scharlau, University of Aberdeen, 2012
Data storage options for mobiles
Mobile Computing
Outline
• Storage in general
• Simple
• Databases
• Frameworks for ORM (object relational mapping)
Bruce Scharlau, University of Aberdeen, 2012
We can place data elsewhere on the network
Use a web service to store data elsewhere – save photos to flickr, files to some other app in the cloud.
Bruce Scharlau, University of Aberdeen, 2012
Can save files automatically, or at user discretion with time values, etc. (twitter, email apps, or photo capture)
Bruce Scharlau, University of Aberdeen, 2012
Mobiles + (Tunnels | Hills) = Lost connection
Need to keep data on the mobile
Could be preferences, scores, something… which isn’t already kept such as contacts, photos, or other data types provided by other apps.
Whatever, you need to determine the best way to store it on the handset
Bruce Scharlau, University of Aberdeen, 2012
Data storage raises issues• How often will the data be accessed?
• How quickly does the data need to appear?
• How often will the data be updated/edited?
• Will the data be added to over time?
• Will the data be deleted?
• How will the data need to be used?
Bruce Scharlau, University of Aberdeen, 2012
These answers will suggest solutions for you
Preference files are a light-weight option
Both Android and iOS use preferences as light-weight option
Bruce Scharlau, University of Aberdeen, 2012
In Android these are not sharable across applications, unless you expose them as a ‘content provider’.
In iOS you can tell NSUserDefaults which domains (apps) to search, so point to another app
We can write larger data to file
• You can either write to a new file, or to a pre-included file under
• Depending upon how much data you want to read/write you have different options.
• You can open socket to remote location and save/read data as stream
• You can build object in memory and then write out object as required and build method to restore too.
Bruce Scharlau, University of Aberdeen, 2012
You can only access files available to the application
Can also serialise objects too
• Store the object as array of bytes is also possible.
• In iOS this only works with specified objects: NSArray, NSDictionary, NSString, NSDate, NSNumber, and NSData (and some of their subclasses)
• In Android you can serialise objects as you would in Java: just need to remember order and do reverse to restore object
Bruce Scharlau, University of Aberdeen, 2012
Java uses RecordStore for objects
Java Mobile uses RecordStore, a byte array for storage
Bruce Scharlau, University of Aberdeen, 2012
Bruce Scharlau, University of Aberdeen, 2012
A RecordStore is NOT a database
A RecordStore is an array of bytes
with MIDP 2.0 a RecordStore can be shared across MIDlet suites
RecordStore names must be unique
Bruce Scharlau, University of Aberdeen, 2012
If multiple apps share an RMS you need to write the logic for this
RMS operations are thread-safe, but threads must still coordinate the reading and writing of data to the same record store.
This is especially important if your RecordStore is shared in a MIDlet suite.
Bruce Scharlau, University of Aberdeen, 2012
When serialising objects might need to consider sorting
• Need a means to determine whether objects are:– EQUIVALENT, FOLLOWS or PRECEDES if
order important, or– EQUIVALENT, BIGGER, SMALLER if size
attribute important
We can also persist data to a db
• Android and iOS can use SQLite db
• Each db is private to the application. In principle you could expose the data, if you expose the application as a content provider.
• In iOS all data can be shared between apps.
Bruce Scharlau, University of Aberdeen, 2012
Android Notepad tutorial uses database
Bruce Scharlau, University of Aberdeen, 2012
Useful db helper class for access and crud details
Android in Action db example covers more complex example
Bruce Scharlau, University of Aberdeen, 2012
Stores locations to database within application as objects
Android in Action app uses db helper classes with sql
Bruce Scharlau, University of Aberdeen, 2012
public static class Location {
public long id; public long lastalert; public int alertenabled; public String zip; // include city and region because geocode is expensive public String city; public String region;
public Location() { }
public Location(final long id, final long lastalert, final int alertenabled, final String zip, final String city, final String region) { this.id = id; this.lastalert = lastalert; this.alertenabled = alertenabled; this.zip = zip; this.city = city; this.region = region; }
Source: android in action, code
Part of DBHelper class showingLocation object
Class also holds crud details to map object to sql
Android in action app maps objects to sql for ease
Bruce Scharlau, University of Aberdeen, 2012
public void insert(final Location location) { ContentValues values = new ContentValues(); values.put("zip", location.zip); values.put("city", location.city); values.put("region", location.region); values.put("lastalert", location.lastalert); values.put("alertenabled", location.alertenabled); this.db.insert(DBHelper.DB_TABLE, null, values); }
public void update(final Location location) { ContentValues values = new ContentValues(); values.put("zip", location.zip); values.put("city", location.city); values.put("region", location.region); values.put("lastalert", location.lastalert); values.put("alertenabled", location.alertenabled); this.db.update(DBHelper.DB_TABLE, values, "_id=" + location.id, null); }
Source: android in action, code
Mapping makes coding easier
SQLite provides advanced db features
• There is transaction support
• You can use prepared statements based on java.sql and set items as have done before – faster and more secure
• You have a cursor to keep track of location within a resultset
Bruce Scharlau, University of Aberdeen, 2012
Can map objects to db
Enables off network use and can sync later when connected
Bruce Scharlau, University of Aberdeen, 2012
Might be pushing limits of device though with extra classes and memory usage
Can read items from network as xml and convert to objects, which map to db
Android storage Summary
• Can use preferences for each app
• Can write/read files as with Java
• Can persist/read items over network (when available)
• Can use SQLite one db per app
Bruce Scharlau, University of Aberdeen, 2012
Can also use ORM systems for more complex data storage
• Object relational mapping provides way to deal with objects and store them as such in a data base automatically
• Framework determines the mappings from objects to tables/rows, etc
Bruce Scharlau, University of Aberdeen, 2012
iOS uses Core Data for ORM
Based on model view control pattern Core Data creates suitable objects for data in the controller that marshals data
Similar to what happens in Ruby on Rails, or Hibernate in Java
Bruce Scharlau, University of Aberdeen, 2012
Android equivalents are available too
• http://ormlite.com/ provides ORM-lite based on Java and SQLite – assumes simple needs
• http://greendao-orm.com is more complex, and faster system that generated ORM per your project needs
Bruce Scharlau, University of Aberdeen, 2012
All depends upon data storage needs
• How often will the data be accessed?
• How quickly does the data need to appear?
• How often will the data be updated/edited?
• Will the data be added to over time?
• Will the data be deleted?
• How will the data need to be used?
Bruce Scharlau, University of Aberdeen, 2012
These answers will suggest solutions for you