Working With Data on Titanium Titanium Data Options: Local Data Remote Data Working With Local Data Titanium Local Storage Options: Application Properties Filesystem Database To use a device's local storage, the following objects are needed: * Titanium.App.Properties is ideal for storing application-related settings * Titanium.Filesystem facilitates file and directory manipulation * Titanium.Database gives access to local SQLite3 databases Each of these enable data to persist on a device across application restarts, power cycles, re-installation and even migration to a new device. What kind of data storage should I use? The decision about which of the three local storage options you choose is usually determined by the following: 1. Application Properties - used when one or all of the following is true: * the data consists of simple key/value pairs * the data is related to the application rather than the user * the data does not require other data in order to be meaningful or useful 2. Filesystem - used when one or all of the following is true: * the data is already provided in file format * the data is an image file 3. Database - used this when one or all of the following is true: * there are many similar data items * items of data relate to each other * you require flexibility over how the data will be presented when you retrieve it * the data accumulates over time, such as transaction, logging or archiving data Application Properties Reading and Writing Properties Titanium.App.Properties has six sets of get/set methods for handling six different data types: getBool() / setBool(): for booleans (true, false) getDouble() / setDouble(): for double-precision floating point numbers getInt() / setInt(): for integers getList() / setList(): for arrays getString() / setString(): for strings > See Sample Code Sample Code result String: This is a string Integer: 10 Boolean: true Double: 10.600000381469727 List: { 'address' : '1 Main St' { 'address' : '2 Main St' { 'address' : '3 Main St' { 'address' : '4 Main St' 'name' 'name' 'name' 'name' : : : : 'Name 'Name 'Name 'Name 1', 2', 3', 4', }, }, }, }, //JSON OUTPUT The myJSON property contains: {"reports":[ {"icon":"www.google.com\ig\images\weather\cloudy.gif","condition":"Cloudy ","city":"Mountain View"}, {"icon":"www.google.com\ig\images\weather\mostly_cloudy.gif","condition": "Cloudy","city":"Washington, DC"}, {"icon":"www.google.com\ig\images\weather\thunderstorm.gif","condition":" Thunderstorm","city":"Brasilia"}]} Filesystem Storage Objects Titanium.Filesystem is the top level Filesystem module used for reading and saving files and directories on the device. Titanium.Filesystem.File is the file object which supported common filesystem based operations such as create, read, write, delete, etc. Properties Data storage locations: applicationDataDirectory: A read-only constant that indicates where your application data directory is located. Place application-specific files in this directory. resourcesDirectory: A read-only constant where your application resources are located tempDirectory: A read-only constant that indicates where your application can place temporary files Filesystem Storage cont... Filesystems Methods getFile(): return a fully-formed file path as a Titanium.Filesystem.File object read(): return the contents of file as blob write(): write the contents to file deleteFile(): delete the file exists(): return true if the file or directory exists on the device move(): move the file to another path rename(): rename the file nativePath(): return the fully resolved native path > See Sample Code SQLite Databases SQLite3 is version 3 of SQLite's SQL-based relational database management system (RDMS), chosen by Apple, Google and RIM to provide local data storage on their mobile devices. Objects Titanium.Database the top level Database module. The Database module is used for creating and accessing the in-application Database. Titanium.Database.DB is the Database instance returned by Titanium.Database.open or Titanium.Database.install. Titanium.Database.ResultSet is the ResultSet instance returned by invoking a database SQL execute. Database Methods open(): open a database. if it doesn't yet exist, create it. execute(): execute a SQL statement against the database and returns a ResultSet. close(): close the database and release resources from memory. SQLite Databases cont... ResultSet Methods isValidRow(): return true if the row is a valid row. fieldByName(): retrieve a row value by field name. next(): iterate to the next row in the result set. returns false if no more results are available. > See Sample Code Working With Remote Data Remote Data in Titanium Your Titanium application can interact with remote servers over HTTP using the HTTPClient object provided through the Titanium.Network namespace. HTTPClient's API mirrors that of the XMLHTTPRequest object in the web browser, so if you have done any Ajax programming in the browser HTTPClient will be familiar to you. Objects Titanium.Network.HTTPClient is the HttpClient instance returned from Titanium.Network.createHTTPClient. This object (mostly) implements the XMLHttpRequest specification. HTTPClient Methods open(): open the request and ready the connection. send(): send the request (Only async is currently supported). SetRequestHeader(): set the request header. Must be called after open but before send. setTimeout(): set the request timeout. > See Sample Code Resources Working with Local Data: http://wiki.appcelerator.org/display/guides/Working+with+Local+Data Working with remote data: http://wiki.appcelerator.org/display/guides/Working+with+Remote+Data API Reference Guides: http://developer.appcelerator.com/apidoc/mobile/latest