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.

Sync is hard: building offline-first Android apps from the ground up

1,732 views

Published on

James Nocentini
Mobile Developer Advocate, UK
Couchbase

Sync is hard: building offline-first Android apps from the ground up

www.droidcon.ae
@droidconae

Published in: Mobile
  • Be the first to comment

Sync is hard: building offline-first Android apps from the ground up

  1. 1. Let’s  build  offline-­‐first  apps! James  Nocentini  |  Developer  Advocate,  Couchbase
  2. 2. ©2014  Couchbase  Inc. Use  case 2 • Clean  UI   • The  user  shouldn’t  have  to  be  online @jamiltz
  3. 3. No  loading,  no  waiting,  nothing.
  4. 4. ©2014  Couchbase  Inc. Options 4 @jamiltz
  5. 5. ©2014  Couchbase  Inc. Options 5 @jamiltz
  6. 6. 6 Sync  Gateway Couchbase  Server
  7. 7. ©2014  Couchbase  Inc. Couchbase  Lite 7 Today  I’d  like  to  talk  about  some  of  the  internals   of  Couchbase  Lite  Android @jamiltz
  8. 8. ©2014  Couchbase  Inc. Saving  a  document 8 Map<String, Object> properties = new HashMap<String, Object>();
 properties.put("type", "list");
 properties.put("title", title);
 properties.put("created_at", currentTimeString);
 properties.put("owner", "profile:" + userId);
 properties.put("members", new ArrayList<String>()); // save to database
 Document document = database.createDocument();
 document.putProperties(properties); @jamiltz
  9. 9. ©2014  Couchbase  Inc. Saving  a  document 9 Document document = database.getDocument(myDocId);
 
 String title = document.getProperty("title");
 
 Map<String, Object> properties = doc.getProperties();
 String owner = (String) properties.get("owner"); @jamiltz
  10. 10. ©2014  Couchbase  Inc. Saving  a  document 10 Document document = database.getDocument(myDocId);
 
 String title = document.getProperty("title");
 
 Map<String, Object> properties = doc.getProperties();
 String owner = (String) properties.get("owner"); @jamiltz
  11. 11. Plain    Old    Java    Objects
  12. 12. Plain    Old    JSON    Objects
  13. 13. ©2014  Couchbase  Inc. It’s  JSON { "_id": "123", "type": "list", "title": "TodoMVC list", "owner": "1234567890", "members": [ "0987654321", "0192837465" ] } 13 @jamiltz
  14. 14. ©2014  Couchbase  Inc. Content  Types JSON ❤ HTTP 14 @jamiltz
  15. 15. ©2014  Couchbase  Inc. What  else? 15 Replicators @jamiltz
  16. 16. ©2014  Couchbase  Inc. Two  Verbs Push    →   Pull        ← 16 @jamiltz
  17. 17. ©2014  Couchbase  Inc. Two  Verbs Push    →  Server   Pull        ←  Server 17 @jamiltz
  18. 18. ©2014  Couchbase  Inc. Two  Verbs Push    →  Couchbase  Sync  Gateway   Pull        ←  Couchbase  Sync  Gateway 18 @jamiltz
  19. 19. ©2014  Couchbase  Inc. Push 19 facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);
 
 Replication push = database.createPushReplication(SYNC_URL);
 push.setAuthenticator(facebookAuthenticator);
 push.setContinuous(true);
 
 push.start();
  20. 20. ©2014  Couchbase  Inc. If  you  were  to  build  your  own ▪ Create  a  job  object  that  has  an  action   ▪ Write  data  to  a  durable  queue   ▪ Run  a  service  to  process  the  queue   ▪ The  service  retries  if  the  network  hiccups  or  app  crashes   ▪ Remove  from  the  queue  once  the  job  is  complete 20 @jamiltz
  21. 21. ©2014  Couchbase  Inc. Replicators 21 Source Target Android  App Sync  Gateway @jamiltz
  22. 22. ©2014  Couchbase  Inc. Replicators Get  the  sequence  ID   of  the  source  database 22 @jamiltz
  23. 23. ©2014  Couchbase  Inc. Replicators 23 Source Target Android  App Sync  Gateway @jamiltz seq:  15 seq:  5
  24. 24. ©2014  Couchbase  Inc. Pull Pull  == 24 Push @jamiltz
  25. 25. ©2014  Couchbase  Inc. Replicators 25 Source Android  App Sync  Gateway @jamiltz Target
  26. 26. ©2014  Couchbase  Inc. Replicators 26 Source Android  App Sync  Gateway @jamiltz Target seq:  15seq:  5
  27. 27. ©2014  Couchbase  Inc. Pull 27 facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);
 
 Replication pull = database.createPullReplication(SYNC_URL);
 pull.setAuthenticator(facebookAuthenticator);
 pull.setContinuous(true);
 
 pull.start();
  28. 28. Demo
  29. 29. Don’t  drain  all  my  battery!
  30. 30. ©2014  Couchbase  Inc. Pull 30 facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);
 
 Replication pull = database.createPullReplication(SYNC_URL);
 pull.setAuthenticator(facebookAuthenticator);
 pull.setContinuous(true);
 
 pull.start();
  31. 31. ©2014  Couchbase  Inc. Solution GCM  is  your  friend   🎉 31 @jamiltz
  32. 32. ©2014  Couchbase  Inc. Data  Flow  Techniques   32 Sync  Gateway changes  feed Push  notifications   worker Continuous  push @jamiltz
  33. 33. ©2014  Couchbase  Inc. Data  Flow  Techniques   33 Sync  Gateway changes  feed Push  notifications   worker Continuous  push GCM  notification GCM  notification @jamiltz
  34. 34. ©2014  Couchbase  Inc. Data  Flow  Techniques   34 Sync  Gateway changes  feed Push  notifications   worker Continuous  push One  shot  pull One  shot  pull @jamiltz
  35. 35. Demo
  36. 36. ©2014  Couchbase  Inc. Conflict  resolution 36 rev-­‐1 rev-­‐2 rev-­‐1 rev-­‐2’ @jamiltz
  37. 37. ©2014  Couchbase  Inc. Conflict  resolution 37 rev-­‐1 rev-­‐2 rev-­‐2’
  38. 38. ©2014  Couchbase  Inc. Conflict  resolution 38 rev-­‐1 rev-­‐2 rev-­‐2’ current  revision
  39. 39. ©2014  Couchbase  Inc. Conflict  resolution 39 rev-­‐1 rev-­‐2 rev-­‐2’
  40. 40. ©2014  Couchbase  Inc. Conflict  resolution 40 rev-­‐1 rev-­‐2 current  revision
  41. 41. P2P Demo
  42. 42. ©2014  Couchbase  Inc. CouchbaseLite  Listener 42 Embedded  lightweight  HTTP  server
  43. 43. ©2014  Couchbase  Inc. Replications 43 Same  Replication  Interface  as  Sync  Gateway
  44. 44. ©2014  Couchbase  Inc. Mesh  Networks 44
  45. 45. Workshops • Push  /  Pull   • Basic  config  for  Sync  Gateway   • And  more…
  46. 46. ©2014  Couchbase  Inc. Summary ▪ Performance   ▪ Ease  of  development   ▪ Live  Queries   ▪ Robustness 46
  47. 47. ©2014  Couchbase  Inc. More  Information 47 James  Nocentini   Developer  Advocate   @jamiltz   ToDoLite  Android   https://github.com/couchbaselabs/ToDoLite-­‐Android   Couchbase  Developer  Forums   http://forums.couchbase.com

×