A couple of months ago, our friends from Smarttrade decided to improve their users experience by a lot. We helped and this is how we did it.
Smarttrade is an awesome product that allows people to manage businesses online. Users have in their pockets, wherever they are, the possibility to manage jobs, receive new leads, accept payments and send receipts. The key part of the last sentence is 'wherever they are', meaning that the app should work flawlessly whether the internet connection is great, poor or completely nonexistent. The experience on the app would have to be the same on every connection status and to do this we needed something to sync data in and out of our mobile apps, making the connection variable a much less important thing for the overall user experience. At this point, some of you might thing that the poor connection scenario that I'm describing is something that belongs to the past but with lots of Smarttrade partners traveling around the UK providing services and lots of different places, we discovered that these situations are much more common that we may think at first.
We started the challenge having no local data storage in the app whatsoever. With this starting point, we needed something to store data on the devices and on top of that a sync mechanism to keep everything synced at all time. Building everything from scratch would be very complex and time consuming so we went with Couchbase.
I'm going to write about what is like to use Couchbase on your mobile apps, leaving the server side aspects of the solution to another post. For now, let's look at the basic infrastructure that Couchbase Mobile offers:
Like I said, we're saving the server card for later. Let's talk about the other two, starting by the
"An embedded NoSQL database that lives locally on mobile devices."
This is from the Couchbase site and the basic result of this is that you'll be using this instead of your Coredata/sqlite/file database.
Couchbase Lite provides a solid API to save, edit and query your data. You can use basic key-value store or document based store, meaning that you can add multiple key-value pairs to a single document.
Couchbase Lite uses a mechanism called views to retrieve data from the database. The downside here is that these are harder to write than SQL queries, you cannot use joins and you have to write your own indexes.
This is where the magical part begins. Sync Gateway will be responsible for moving documents in and out of your local database and sync everything with the Couchbase Server.
In order to catch the database changes that the Sync Gateway will do on your local database, you can add listeners to both specific documents or the entire database. This is super useful to update your interface as soon as some change is made somewhere on your system. For instance, if your app is (yet another) task list, you can add a database listener to refresh a list controller on your app whenever some new task document hits the local database. And yes, you can finally remove your until now mandatory refresh button/gesture.
The Sync Gateway knows what to push to your device due to a channel setting that your documents may have and it can handle document conflicts with a system of revisions that local documents keep a record of. These helper mechanisms are really useful cause they make sure you're getting correct data on your local database all the time and you can access and modify these revisions and channels in order to achieve a more complex behavior that you might need. Couchbase Lite uses an abstraction for the communication channels used to interact with the Sync Gateway, they call it replications. You can either set replications to keep your app listening to changes on documents or you can use replications to push a specific document to the server.
Couchbase Lite provides a handy base model on their iOS lib, on Android, you have to write your own model to create an abstraction between the documents stored on your database and your controllers and views.
Either way, it shouldn't be difficult to adapt Couchbase Lite to your project or adapt your existing code base to it.
For us, and since that before Couchbase integration the app was fed with json, we had to translate our json structured models to the hashmaps that the document object uses.
In this post, I wanted to give an overview about how can we use this solution to improve our mobile apps. If you have an app that needs to perform well both offline and online, this might be a good solution for you. We'll also try to write a bit about Couchbase server in the future.
Here're some code examples really handy to get you started in the Couchbase ecosystem: