softshake 2013 - let's take this offline

42
Let's take this offline How to build mobile apps that work offline?

Upload: claire-reynaud

Post on 10-Jun-2015

711 views

Category:

Technology


3 download

DESCRIPTION

How to build mobile apps that work offline? The code examples are available on github : https://github.com/creynaud/notes-iphone-app https://github.com/creynaud/notes-server A blog post on the same subject: https://www.clairereynaud.net/blog/adding-offline-mode-to-your-mobile-app/

TRANSCRIPT

Page 1: Softshake 2013 - Let's take this offline

Let's take this offlineHow to build mobile apps that work offline?

Page 2: Softshake 2013 - Let's take this offline

www.babelbytes.com

@ClaireReynaudhttps://github.com/creynaud

Page 3: Softshake 2013 - Let's take this offline

What about you?

Page 4: Softshake 2013 - Let's take this offline

Subject of this talkGeneral how-to for making your apps workoffline

Examples:iOSCouchdb REST APIDjango REST Framework

Page 5: Softshake 2013 - Let's take this offline

What do I mean by"work offline"?

Page 6: Softshake 2013 - Let's take this offline

No network or slow network

Users shouldn't have to wait toread content if they already

accessed it

Page 7: Softshake 2013 - Let's take this offline

No network or slow network

Users should be able to postcontent all the time

Page 8: Softshake 2013 - Let's take this offline

Why make mobileapps work offline?

Page 9: Softshake 2013 - Let's take this offline

Facebook app one year ago

Page 10: Softshake 2013 - Let's take this offline

We want a betteruser experience!

Page 11: Softshake 2013 - Let's take this offline

Now users expect content whenoffline

Page 12: Softshake 2013 - Let's take this offline

Mobile networks are differentLatency is high

Speed is very inconsistent, which is bad in termsof UX

Watch the "Faster Websites: Crash Course onFrontend Performance (Part 1/2)" talk fromDevoxx 2012

Page 13: Softshake 2013 - Let's take this offline

Your app in the wildTry your app with the Network Conditioner

Page 14: Softshake 2013 - Let's take this offline

Let's implement anEvernote-like app

Well, the text notes part ;)

Page 15: Softshake 2013 - Let's take this offline

In 3 stepsReading notes while offline1.Creating notes while offline2.Updating notes while offline and solving conflicts3.

Page 16: Softshake 2013 - Let's take this offline

Put a revision in yournote objects!

Page 17: Softshake 2013 - Let's take this offline

Typical mobile app architecture

Page 18: Softshake 2013 - Let's take this offline

1. Reading noteswhile offline

Page 19: Softshake 2013 - Let's take this offline

Reading notes while offline

Read JSON documents from alocal cache on the mobile device

Page 20: Softshake 2013 - Let's take this offline

Wait, HTTP has acaching mechanism,

right?

Page 21: Softshake 2013 - Let's take this offline

You need a higher level cacheYou may want to do offline search requests

You may want to do offline edition

You can use CoreData on top of SQLite, oranother storage mechanism.

Depending on your needs, storing the rawJSON document in a key/value store may beenough.

Page 22: Softshake 2013 - Let's take this offline

What to keep from HTTP cachingthough?

For example, I don't want to re-download a givenversion of a JSON document if I already have it in theHTTP cache.

This is possible with HTTP caching headers:

Cache-Control

Etag and If-None-Match

or Last-Modified and If-Modified-Since

Page 23: Softshake 2013 - Let's take this offline

Let's see how itworks with Couchdb

REST API

Page 24: Softshake 2013 - Let's take this offline

What is Couchdb?"A database for the web"

Couchdb is a NoSQL database

It stores JSON documents

It is schemaless

Page 25: Softshake 2013 - Let's take this offline

Why do I talk about Couchdb?

It is a great example for building a REST API

Spoiler alert: otherwise I don't use Couchdb

Page 26: Softshake 2013 - Let's take this offline

Time to play withCouchdb

Page 27: Softshake 2013 - Let's take this offline

HTTP GET caching with ETAG

Blog post about NSURLCache, HTTP caching policiesand ETAG

Page 28: Softshake 2013 - Let's take this offline

2. Creating noteswhile offline

Page 29: Softshake 2013 - Let's take this offline

Creating notes while offlineStore (e.g in SQLite) the JSON document thatneeds to be posted

1.

Try to post the JSON document to the server inthe background

2.

Mark the JSON document has successfullyposted only if POST succeeds

3.

In case of failure, retry to post theJSON document during next sync with the server

4.

Page 30: Softshake 2013 - Let's take this offline

3. Solving conflictson notes edition

Page 31: Softshake 2013 - Let's take this offline

Solving conflicts on notes edition

When you let your user update notes while offline,conflicts will show up (even if there is no multi-useredition).

Page 32: Softshake 2013 - Let's take this offline

Conflict detectionshould be built in

your REST API!

Page 33: Softshake 2013 - Let's take this offline

Again, time to playwith Couchdb

Page 34: Softshake 2013 - Let's take this offline

What about Androidapps?

Page 35: Softshake 2013 - Let's take this offline

What about Android apps?More or less the same:

Put a uuid and a revision on all your JSONdocuments

Offline read: store your JSON Documents inSQLite

Offline create: POST to the server in thebackground and retry if needed

Offline update: handle conflicts

Try to leverage HTTP caching

Page 36: Softshake 2013 - Let's take this offline

What abouthybrid/web apps?

Page 37: Softshake 2013 - Let's take this offline

What about hybrid/web apps?SQLite native storage: it may not be possible toaccess SQLite depending on the framework youuse

HTML5 localstorage

Page 38: Softshake 2013 - Let's take this offline

What if I don't wantto use Couchdb?

Page 39: Softshake 2013 - Let's take this offline

You can use whatever you wantThis is what I use, but it's really up to you

Page 40: Softshake 2013 - Let's take this offline

Let's see how it works with DjangoREST framework

Put a uuid and a revision on your Note objects

Reject PUT or DELETE request if it does notprovide a revision (400 Bad request)

Reject PUT or DELETE request if the revision isnot the current one (409 Conflict)

Everything else comes out of the box!https://github.com/creynaud/notes-serverhttps://awesomenotes.herokuapp.com/api/

Page 41: Softshake 2013 - Let's take this offline

SummaryPut a uuid and a revision on all your JSONdocuments

Offline read: store your JSON Documents inSQLite

Offline create: POST to the server in thebackground and retry if needed

Offline update: handle conflicts

Try to leverage HTTP caching (Cache-Control,Etag and If-None-Match headers)

Page 42: Softshake 2013 - Let's take this offline

Thanks! Questions?