Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Working with Data
in Titanium

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
can be frustrating

PRATIK PATEL | CTO
Use the FUNC, Luke
• functional programming in
• break down the problem
• json is very flexible

JavaScript

PRATIK PATEL |...
take this

[{
      "id":30445,
      "speakerNames":"Pratik Patel",
      "smallImageSq":"/s/images/bio/
14890_Patel_2011...
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 lis...
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
SQLite
• Embedded sql-92 compliant db
• file based
• fast(!)

PRATIK PATEL | CTO
sqlite
var	
  db	
  =	
  Titanium.Database.open('mydb');	
  
!
db.execute('CREATE	
  TABLE	
  IF	
  NOT	
  EXISTS	
  DATAB...
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 P...
SCULEJS / MongloDB
• Mongodb-like
• relationships
• advanced queries

PRATIK PATEL | CTO
SculeJS
var scollection = scule.factoryCollection('scule+dummy://test',
{secret:’mysecretkey'});	
!
scollection.save({ nam...
now the boring stuff

PRATIK PATEL | CTO
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 ...
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

PRAT...
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 PATE...
LOCAL STORAGE
• SQLite = File
• JSON can use Ti.App.Properties
• Props get loaded into MEMORY
• Not GC-able

PRATIK PATEL ...
WHAT ABOUT ALLOY?

PRATIK PATEL | CTO
i’m glad you asked

PRATIK PATEL | CTO
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...
ALLOY + SYNC
• Alloy uses Backbone models
• Backbone adapters
• Titanium specific adapters

PRATIK PATEL | CTO
AU REVOIR

PRATIK PATEL
@PRPATEL

PRATIK@mypatelspace.com
Working with Data and Titanium
Upcoming SlideShare
Loading in …5
×

Working with Data and Titanium

2,192 views

Published on

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

  • Be the first to comment

Working with Data and Titanium

  1. 1. Working with Data in Titanium Presentation by: PRATIK PATEL | CTO | TripLingo Labs | @prpatel PRATIK@mypatelspace.com
  2. 2. @prpatel @TripLingo
  3. 3. TOPICS • working with data • Strategies: SQL & NOSQL • architecture • synchronization PRATIK PATEL | CTO
  4. 4. TRANSFORMING DATA PRATIK PATEL | CTO
  5. 5. can be frustrating PRATIK PATEL | CTO
  6. 6. Use the FUNC, Luke • functional programming in • break down the problem • json is very flexible JavaScript PRATIK PATEL | CTO
  7. 7. take 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 dan gerous, but feel like the whole prototypical language thing is still a mys tery. In this session, we'll go from basic JavaScript to advanced JavaScri pt. We'll discuss and code modular JavaScript with CommonJS. We'll look in to the details of a prototype language and discuss things like parasitic i nheritance. 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 spee d with the latest and greatest JavaScript techniques and tools. Whether yo u're building client side JavaScript with HTML5 or Appcelerator Titanium,  or serverside 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"    } make this PRATIK PATEL | CTO
  8. 8. underscore.js • use it • master it • think functional PRATIK PATEL | CTO
  9. 9. 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
  10. 10. flatten •Flattens a nested array (the nesting can be to any depth) PRATIK PATEL | CTO
  11. 11. sortBy • sorts (duh!) • can sort arrays and even an array of objects! PRATIK PATEL | CTO
  12. 12. live coding
  13. 13. SQLite & NoSQL
  14. 14. SQLite • Embedded sql-92 compliant db • file based • fast(!) PRATIK PATEL | CTO
  15. 15. sqlite var  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
  16. 16. SQLite • Pre-bundling data • fast! • structured data PRATIK PATEL | CTO
  17. 17. NOSQL OPTIONS • SCuleJS - like mongodb • MongloDB • plain json as we saw! PRATIK PATEL | CTO
  18. 18. PLAIN JSON • can get alot of mileage out of this • small = store using Ti.App.Properties! • big = store in a file PRATIK PATEL | CTO
  19. 19. SCULEJS / MongloDB • Mongodb-like • relationships • advanced queries PRATIK PATEL | CTO
  20. 20. 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
  21. 21. now the boring stuff PRATIK PATEL | CTO
  22. 22. SYNCHRONIZATION PRATIK PATEL | CTO
  23. 23. PLAIN JSON + REST • Standard REST endpoints • You manage the sync yourself PRATIK PATEL | CTO
  24. 24. 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
  25. 25. ARCHITECTURAL CONSIDERATIONS PRATIK PATEL | CTO
  26. 26. PERFORMANCE • Large datasets - handle with care • “cache” operations if possible PRATIK PATEL | CTO
  27. 27. PERFORMANCE • SQLite is native code = queries fast • data extraction = expensive PRATIK PATEL | CTO
  28. 28. PERFORMANCE • JSON queries slow (non-indexed iteration) • data extraction cost is ZERO • SculeJS has indexed find ops PRATIK PATEL | CTO
  29. 29. NETWORK • Know what your users are running on - 3G? - Wireless? PRATIK PATEL | CTO
  30. 30. NETWORK • Speed over 3G networks is usually good • LATENCY is the killer! • Each request => 150-500ms latency PRATIK PATEL | CTO
  31. 31. LOCAL STORAGE • SQLite = File • JSON can use Ti.App.Properties • Props get loaded into MEMORY • Not GC-able PRATIK PATEL | CTO
  32. 32. WHAT ABOUT ALLOY? PRATIK PATEL | CTO
  33. 33. i’m glad you asked PRATIK PATEL | CTO
  34. 34. ALLOY + DATA • Alloy uses Backbone models • Backbone uses…. PRATIK PATEL | CTO
  35. 35. UNDERSCORE! PRATIK PATEL | CTO
  36. 36. ALLOY + UNDERSCORE • The code demo shown earlier just works with ALLOY: • collection.map • collection.sortBy PRATIK PATEL | CTO
  37. 37. ALLOY + SYNC • Alloy uses Backbone models • Backbone adapters • Titanium specific adapters PRATIK PATEL | CTO
  38. 38. AU REVOIR PRATIK PATEL @PRPATEL PRATIK@mypatelspace.com

×