Sync is hard: building offline-first Android apps from the ground up
-
Upload
droidcon-dubai -
Category
Mobile
-
view
90 -
download
0
Transcript of Sync is hard: building offline-first Android apps from the ground up
©2014 Couchbase Inc.
Couchbase Lite
7
Today I’d like to talk about some of the internals of Couchbase Lite Android
@jamiltz
©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 databaseDocument document = database.createDocument();document.putProperties(properties);
@jamiltz
©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
©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
©2014 Couchbase Inc.
It’s JSON
{ "_id": "123", "type": "list", "title": "TodoMVC list", "owner": "1234567890", "members": [
"0987654321","0192837465"
]}
13
@jamiltz
©2014 Couchbase Inc.
Two Verbs
Push → Couchbase Sync Gateway Pull ← Couchbase Sync Gateway
18
@jamiltz
©2014 Couchbase Inc.
Push
19
facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);Replication push = database.createPushReplication(SYNC_URL);push.setAuthenticator(facebookAuthenticator);push.setContinuous(true);push.start();
©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
©2014 Couchbase Inc.
Pull
27
facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);Replication pull = database.createPullReplication(SYNC_URL);pull.setAuthenticator(facebookAuthenticator);pull.setContinuous(true);pull.start();
©2014 Couchbase Inc.
Pull
30
facebookAuthenticator = AuthenticatorFactory.createFacebookAuthenticator(accessToken);Replication pull = database.createPullReplication(SYNC_URL);pull.setAuthenticator(facebookAuthenticator);pull.setContinuous(true);pull.start();
©2014 Couchbase Inc.
Data Flow Techniques
32
Sync Gateway
changes feed
Push notifications worker
Continuous push
@jamiltz
©2014 Couchbase Inc.
Data Flow Techniques
33
Sync Gateway
changes feed
Push notifications worker
Continuous push
GCM notification
GCM notification
@jamiltz
©2014 Couchbase Inc.
Data Flow Techniques
34
Sync Gateway
changes feed
Push notifications worker
Continuous push
One shot pull
One shot pull
@jamiltz
©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