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.
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
and feed the credentials back to CouchbaseLite to authenticate any replications,
which are setup.
And with those little pieces of code we actually have a working sync setup.
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.
So all there is left to do is creating a new object of this kind and save it.
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
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.
In TodoLite-Motion I set it
up to be a vendored Xcode project, so it can easily be compiled and changed when
And that’s it, a basic project up and running with everything needed to have a
synced and shared database in RubyMotion.