Working with Data and Titanium

Post on 30-Jun-2015

1,242 views 1 download

description

Slides from Pratik Patel from the February 2014, London Titanium Meetup.

transcript

in Titanium Working with Data

Presentation by: PRATIK PATEL | CTO | TripLingo Labs | @prpatel PRATIK@mypatelspace.com

@prpatel @TripLingo

TOPICS• working with data

• Strategies: SQL & NOSQL

• architecture

• synchronization

PRATIK PATEL | CTO

TRANSFORMING DATA

PRATIK PATEL | CTO

PRATIK PATEL | CTO

can be frustrating

Use the FUNC, Luke• functional programming in JavaScript

• break down the problem

• json is very flexible

PRATIK PATEL | CTO

PRATIK PATEL | CTO

take this

make this

[{       "id":30445,       "speakerNames":"Pratik Patel",       "smallImageSq":"/s/images/bio/14890_Patel_20110408_052535_small_sq.jpg",       "mediumImageSq":"/s/images/bio/14890_Patel_20110408_052535_medium_sq.jpg",       "roomId":1554,       "ordinal":2,       "showId":327,       "slotId":7956,       "featured":true,       "modified":"2013-12-17T21:19:05",       "topicId":3193,       "eventId":null,       "title":"Advanced JavaScript for Java Devs",       "summary":"So you think you've picked up enough JavaScript to be dangerous, but feel like the whole prototypical language thing is still a mystery. In this session, we'll go from basic JavaScript to advanced JavaScript. We'll discuss and code modular JavaScript with CommonJS. We'll look into the details of a prototype language and discuss things like parasitic inheritance. We'll also look at JavaScript libraries that will help you get the most out of JavaScript - not jQuery, but a library like UnderscoreJS and SugarJS.",       "detail":"This is a fast paced session meant to bring you up to speed with the latest and greatest JavaScript techniques and tools. Whether you're building client side JavaScript with HTML5 or Appcelerator Titanium, or server-side JavaScript with node.js, you'll come away with knowledge and patterns for how the pro's use JavaScript for building real apps.",       "prerequisite":null,       "workshopRequirements":null,       "endTime":"2014-03-07T14:45:00",       "startTime":"2014-03-07T13:15:00",       "speakerIds":[          14890       ],       "missingSlides":false,       "dayNumber":0,       "startTimeString":"Fri 01:15 PM",       "roomName":"Room 2"    }

underscore.js• use it

• master it

• think functional

PRATIK PATEL | CTO

map • map is the name of a higher-order function that applies a given function to each element of a list, returning a list of results

• like doing a transformation

PRATIK PATEL | CTO

flatten •Flattens a nested array (the nesting can be to any depth)

PRATIK PATEL | CTO

sortBy • sorts (duh!)

• can sort arrays and even an array of objects!

PRATIK PATEL | CTO

live coding

SQLite &

NoSQL

PRA

SQLite• Embedded sql-92 compliant db

• file based

• fast(!)

PRATIK PATEL | CTO

sqlitevar  db  =  Titanium.Database.open('mydb');  !db.execute('CREATE  TABLE  IF  NOT  EXISTS  DATABASETEST    (ID  INTEGER,  NAME  TEXT)');  db.execute('DELETE  FROM  DATABASETEST');  !db.execute('INSERT  INTO  DATABASETEST  (ID,  NAME  )  VALUES(?,?)',1,'Name  1');  db.execute('UPDATE  DATABASETEST  SET  NAME  =  ?  WHERE  ID  =  ?',  updateName,  updateId);  var  rows  =  db.execute('SELECT  *  FROM  DATABASETEST');  !while  (rows.isValidRow())  {     Titanium.API.info('ID:  '  +  rows.field(0)  +  '  NAME:  '  +          rows.fieldByName('name')  +  '  COLUMN  NAME  '  +  rows.fieldName(0));     rows.next();  }

PRATIK PATEL | CTO

SQLite• Pre-bundling data

• fast!

• structured data

PRATIK PATEL | CTO

NOSQL OPTIONS• SCuleJS - like mongodb

• MongloDB

• plain json as we saw!

PRATIK PATEL | CTO

PLAIN JSON• can get alot of mileage out of this

• small = store using Ti.App.Properties!

• big = store in a file

PRATIK PATEL | CTO

SCULEJS / MongloDB• Mongodb-like

• relationships

• advanced queries

PRATIK PATEL | CTO

SculeJS

var scollection = scule.factoryCollection('scule+dummy://test', {secret:’mysecretkey'});!scollection.save({ name : ‘pratik', session : 'javascript'}, { name : ‘ket', session : 'design'});!scollection.find({$within:{name:’pratik’}}, {$limit:10, $sort:{name:-1}}, function(results) { // do something with results here});

PRATIK PATEL | CTO

PRATIK PATEL | CTO

now the boring stuff

SYNCHRONIZATION

PRATIK PATEL | CTO

PLAIN JSON + REST• Standard REST endpoints

• You manage the sync yourself

PRATIK PATEL | CTO

SQLite• You manage the sync yourself

• Manual conversion to/from SQL

• ORM’s for Titanium

• BUT: why not send the actual SQLite file over the wire???

PRATIK PATEL | CTO

ARCHITECTURAL CONSIDERATIONS

PRATIK PATEL | CTO

PERFORMANCE• Large datasets - handle with care

• “cache” operations if possible

PRATIK PATEL | CTO

PERFORMANCE• SQLite is native code = queries fast

• data extraction = expensive

PRATIK PATEL | CTO

PERFORMANCE• JSON queries slow (non-indexed iteration)

• data extraction cost is ZERO

• SculeJS has indexed find ops

PRATIK PATEL | CTO

NETWORK• Know what your users are running on - 3G? - Wireless?

PRATIK PATEL | CTO

NETWORK• Speed over 3G networks is usually good

• LATENCY is the killer!

• Each request => 150-500ms latency

PRATIK PATEL | CTO

LOCAL STORAGE• SQLite = File

• JSON can use Ti.App.Properties

• Props get loaded into MEMORY

• Not GC-able

PRATIK PATEL | CTO

WHAT ABOUT ALLOY?

PRATIK PATEL | CTO

PRATIK PATEL | CTO

i’m glad you asked

ALLOY + DATA• Alloy uses Backbone models

• Backbone uses….

PRATIK PATEL | CTO

UNDERSCORE!

PRATIK PATEL | CTO

ALLOY + UNDERSCORE• The code demo shown earlier just works with ALLOY:

• collection.map

• collection.sortBy

PRATIK PATEL | CTO

ALLOY + SYNC• Alloy uses Backbone models

• Backbone adapters

• Titanium specific adapters

PRATIK PATEL | CTO

AU REVOIRPRATIK PATEL

@PRPATEL PRATIK@mypatelspace.com