140 likes | 266 Views
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.
E N D
CS569 Selected Topics in Software EngineeringSpring 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 • 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
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
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++) { …
Pause to see some code Another little tour • Populating table from list of properties • Firing custom events • Storing and updating properties
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.
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();
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
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
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)' );
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();
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);
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