Working with Data and Titanium
-
Upload
londontitanium -
Category
Technology
-
view
1.243 -
download
1
description
Transcript of Working with Data and Titanium
![Page 1: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/1.jpg)
in Titanium Working with Data
Presentation by: PRATIK PATEL | CTO | TripLingo Labs | @prpatel [email protected]
![Page 2: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/2.jpg)
@prpatel @TripLingo
![Page 3: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/3.jpg)
TOPICS• working with data
• Strategies: SQL & NOSQL
• architecture
• synchronization
PRATIK PATEL | CTO
![Page 4: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/4.jpg)
TRANSFORMING DATA
PRATIK PATEL | CTO
![Page 5: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/5.jpg)
PRATIK PATEL | CTO
can be frustrating
![Page 6: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/6.jpg)
Use the FUNC, Luke• functional programming in JavaScript
• break down the problem
• json is very flexible
PRATIK PATEL | CTO
![Page 7: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/7.jpg)
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" }
![Page 8: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/8.jpg)
underscore.js• use it
• master it
• think functional
PRATIK PATEL | CTO
![Page 9: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/9.jpg)
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
![Page 10: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/10.jpg)
flatten •Flattens a nested array (the nesting can be to any depth)
PRATIK PATEL | CTO
![Page 11: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/11.jpg)
sortBy • sorts (duh!)
• can sort arrays and even an array of objects!
PRATIK PATEL | CTO
![Page 12: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/12.jpg)
![Page 13: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/13.jpg)
live coding
![Page 14: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/14.jpg)
SQLite &
NoSQL
PRA
![Page 15: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/15.jpg)
SQLite• Embedded sql-92 compliant db
• file based
• fast(!)
PRATIK PATEL | CTO
![Page 16: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/16.jpg)
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
![Page 17: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/17.jpg)
SQLite• Pre-bundling data
• fast!
• structured data
PRATIK PATEL | CTO
![Page 18: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/18.jpg)
NOSQL OPTIONS• SCuleJS - like mongodb
• MongloDB
• plain json as we saw!
PRATIK PATEL | CTO
![Page 19: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/19.jpg)
PLAIN JSON• can get alot of mileage out of this
• small = store using Ti.App.Properties!
• big = store in a file
PRATIK PATEL | CTO
![Page 20: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/20.jpg)
SCULEJS / MongloDB• Mongodb-like
• relationships
• advanced queries
PRATIK PATEL | CTO
![Page 21: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/21.jpg)
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
![Page 22: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/22.jpg)
PRATIK PATEL | CTO
now the boring stuff
![Page 23: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/23.jpg)
SYNCHRONIZATION
PRATIK PATEL | CTO
![Page 24: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/24.jpg)
PLAIN JSON + REST• Standard REST endpoints
• You manage the sync yourself
PRATIK PATEL | CTO
![Page 25: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/25.jpg)
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
![Page 26: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/26.jpg)
ARCHITECTURAL CONSIDERATIONS
PRATIK PATEL | CTO
![Page 27: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/27.jpg)
PERFORMANCE• Large datasets - handle with care
• “cache” operations if possible
PRATIK PATEL | CTO
![Page 28: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/28.jpg)
PERFORMANCE• SQLite is native code = queries fast
• data extraction = expensive
PRATIK PATEL | CTO
![Page 29: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/29.jpg)
PERFORMANCE• JSON queries slow (non-indexed iteration)
• data extraction cost is ZERO
• SculeJS has indexed find ops
PRATIK PATEL | CTO
![Page 30: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/30.jpg)
NETWORK• Know what your users are running on - 3G? - Wireless?
PRATIK PATEL | CTO
![Page 31: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/31.jpg)
NETWORK• Speed over 3G networks is usually good
• LATENCY is the killer!
• Each request => 150-500ms latency
PRATIK PATEL | CTO
![Page 32: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/32.jpg)
LOCAL STORAGE• SQLite = File
• JSON can use Ti.App.Properties
• Props get loaded into MEMORY
• Not GC-able
PRATIK PATEL | CTO
![Page 33: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/33.jpg)
WHAT ABOUT ALLOY?
PRATIK PATEL | CTO
![Page 34: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/34.jpg)
PRATIK PATEL | CTO
i’m glad you asked
![Page 35: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/35.jpg)
ALLOY + DATA• Alloy uses Backbone models
• Backbone uses….
PRATIK PATEL | CTO
![Page 36: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/36.jpg)
UNDERSCORE!
PRATIK PATEL | CTO
![Page 37: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/37.jpg)
ALLOY + UNDERSCORE• The code demo shown earlier just works with ALLOY:
• collection.map
• collection.sortBy
PRATIK PATEL | CTO
![Page 38: Working with Data and Titanium](https://reader034.fdocuments.us/reader034/viewer/2022042816/559221c01a28abcd068b45a7/html5/thumbnails/38.jpg)
ALLOY + SYNC• Alloy uses Backbone models
• Backbone adapters
• Titanium specific adapters
PRATIK PATEL | CTO