1 / 14

Mobile: Basic UIs and local data

CS569 Selected Topics in Software Engineering Spring 2012. Mobile: Basic UIs and local data. Before we dive into local data…. Tour of a simple application. Code organization Creating a window Creating widgets Catching events Creating table view. Innumerable reasons to store data locally.

kaden-beach
Download Presentation

Mobile: Basic UIs and local data

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS569 Selected Topics in Software EngineeringSpring 2012 Mobile: Basic UIs and local data

  2. Before we dive into local data… Tour of a simple application. • Code organization • Creating a window • Creating widgets • Catching events • Creating table view

  3. Innumerable reasons to store data locally • Storing user preferences • Storing (encrypted) username and password to reduce need for retyping • Storing records that are waiting to propagate back up to the server • Storing content in a local cache to improve performance • Storing rich content that is only needed locally

  4. Options for storing data • Property-value list • Every value is referenced by its property name • Files and blobs • Good for storing rich content • Structured data • Basically your own local database server

  5. Saving properties: key functions • Ti.App.Properties.setString(propName, propValue); • Ti.App.Properties.removeProperty(propName); • varallProperties = Ti.App.Properties.listProperties().sort(); for(vari = 0; i < allProperties.length; i++) { …

  6. Pause to see some code Another little tour • Populating table from list of properties • Firing custom events • Storing and updating properties

  7. Some caveats… • System-defined properties cannot be edited reliably • System-defined properties might appear after execution begins (especially in simulator) • All of YOUR app-defined properties will be loaded when the app starts up… • So don’t save hundreds and hundreds of properties, because that would slow startup.

  8. Saving files: key functions • To list and read files: vardirPath = Ti.Filesystem.getApplicationDataDirectory(); vardirObject = Ti.Filesystem.getFile(dirPath); varallFiles = dirObject.getDirectoryListing(); for(vari = 1; i < allFiles.length; i++) { varfilename = allFiles[i]; varfileObject = Ti.Filesystem.getFile(dirPath, filename); varfileContent = fileObject.read(); // string or blob • To store: fileObject.write(filevalue, false); • To delete: fileObject.deleteFile();

  9. Other useful notes • One performance-related file will be automatically created by the simulator. • Other useful directories you can read/write: • Resources subdirectory (generally read-only) • External storage • Flash card • Android only • Check with isExternalStoragePresent first

  10. Structured data • Essentially a mini SqlLite database • Embedded database server • Limited data types: • NULL, INTEGER, REAL, TEXT, BLOB • But really you can put any kind of data into any column (except for INTEGER primary keys) - it will be coerced • Primary operators: • =, !=, <, >, <=, >=, IN, NOT IN, BETWEEN • ORDER BY and GROUP BY are generally reliable • Nice built-in functions http://www.sqlite.org/lang_corefunc.html

  11. Connecting to a database • First time you open() a database, it will be initialized. Use CREATE TABLEIF NOT EXISTS. Close connection after each operation (yes). vardb = Ti.Database.open('mydb'); db.execute( 'CREATE TABLE IF NOT EXISTS proplist(pid '+ 'INTEGER PRIMARY KEY, pname TEXT, '+ 'pvalueTEXT)' );

  12. Retrieving all rows in a table var items = []; var db = Ti.Database.open('mydb'); var rs = db.execute('SELECT * from proplist'); while(rs.isValidRow()) { var item = { pid : rs.fieldByName('pid'), pname : rs.fieldByName('pname'), pvalue : rs.fieldByName('pvalue') }; items.push(item); rs.next(); } db.close();

  13. Insert, update, delete db.execute('INSERT INTO proplist'+ '(pname,pvalue) VALUES (?,?)', pname, pvalue); pid = db.lastInsertRowId; db.execute('UPDATE proplist SET pname=?, '+ 'pvalue=? WHERE id=?', pname, pvalue, pid); db.execute('DELETE FROM proplist WHERE pid=?', pid);

  14. Important considerations • Security • Databases are not encrypted: if you store private data, be sure to encrypt it first • Performance • Indexes are not created automatically except for primary key; to create a custom index, see http://www.sqlite.org/lang_createindex.html • Usability • For maximal usability, explore how to use platform-specific APIs so your app matches the usual look and feel of the platform

More Related