Presentation is loading. Please wait.

Presentation is loading. Please wait.

Working With Data on Titanium Local Data Remote Data Titanium Data Options:

Similar presentations


Presentation on theme: "Working With Data on Titanium Local Data Remote Data Titanium Data Options:"— Presentation transcript:

1 Working With Data on Titanium Local Data Remote Data Titanium Data Options:

2 Working With Local Data Application Properties Filesystem Database Titanium Local Storage Options: 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.

3 What kind of data storage should I use? 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 The decision about which of the three local storage options you choose is usually determined by the following:

4 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

5 Sample Code result String: This is a string Integer: 10 Boolean: true Double: 10.600000381469727 List: { 'address' : '1 Main St' 'name' : 'Name 1', }, { 'address' : '2 Main St' 'name' : 'Name 2', }, { 'address' : '3 Main St' 'name' : 'Name 3', }, { 'address' : '4 Main St' 'name' : 'Name 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"}]}

6 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. 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 Properties

7 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 Filesystem Storage cont... > See Sample Code Filesystems Methods

8 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. 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. Database Methods

9 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. ResultSet Methods > See Sample Code SQLite Databases cont...

10 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. 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. HTTPClient Methods > See Sample Code

11 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


Download ppt "Working With Data on Titanium Local Data Remote Data Titanium Data Options:"

Similar presentations


Ads by Google