Recently Couchbase Lite 1.0 got released, and you can find anything about it at
the Couchbase Mobile Developer Portal.
As I’ve been quite interested in RubyMotion since some time, I decided to check
how easy it would be to take CouchbaseLite for a spin on RubyMotion, and I have
to say it is quite easy. Besides some small problems with RubyMotion handling
of blocks everything worked as expected. The full project is up on
github for you to checkout,
but I’d like to walk through some of the parts worth noting here.
Getting the database ready
First things first, opening a database file for CouchbaseLite works as expected
simply porting over what is presented in the examples.
https://gist.github.com/sideshowcoder/48ef31df4bad66c1b152.js
This database object can now be passed down to wherever it is needed. So now is
the time to get onto one of the big features of CouchbaseLite, which is sync. To
get this working we authenticate against Facebook using ACAccountStore
https://gist.github.com/sideshowcoder/8d9a3200a97e0364f9eb.js
and feed the credentials back to CouchbaseLite to authenticate any replications,
which are setup.
https://gist.github.com/sideshowcoder/b48433984955b0bb99cb.js
And with those little pieces of code we actually have a working sync setup.
Data in…
A syncing database is nice, but without data in it also kind of useless ;), so
let’s get something in there. CouchbaseLite actually provides a nice layer on
top here, letting you define models for your data which you can then read and
write like you would with any ORM. And it’s more than that, you can actually use
those as well to directly bind to a view, but more about that later. First let’s
get some data in there, by first creating a model to represent this.
https://gist.github.com/sideshowcoder/a714de4e703ef1b9feaf.js
So all there is left to do is creating a new object of this kind and save it.
https://gist.github.com/sideshowcoder/cb9049a7d4d56fcfa72f.js
And that’s it, we can now store items in our database fairly simple.
… and data out
With this we can take advantage of one of the other big features of
CouchbasLite, at least to me, which is using CouchbaseLite as an
TableViewDataSource. This cuts out huge amounts of boilerplate code, so
https://gist.github.com/sideshowcoder/a18925f3f59b878bdd91.js
will get everything setup, and make sure that the display is always up to date.
One more (sad) thing…
Sadly right now, due to a bug in the handling of Procs in RubyMotion, it does
not allow you to setup map and reduce blocks via Ruby. This will probably be
resolved by Rubymotion soon, but for now it means you have to setup the blocks
from Objective-C. But don’t worry it’s not hard, basically it’s just the code
for the block itself which is pretty easy to grasp.
https://gist.github.com/sideshowcoder/399a22d8544a92de27c3.js
In TodoLite-Motion I set it
up to be a vendored Xcode project, so it can easily be compiled and changed when
needed.
The end
And that’s it, a basic project up and running with everything needed to have a
synced and shared database in RubyMotion.