A quick look at Dropbox’s new Datastore API

Jayme Singleton


Dropbox released a new API for mobile app developers yesterday at their DBX conference: a Datastore API which allows you to sync data between a user’s devices in real-time, via their dropbox account.

As soon as the keynote ended, we rushed back to Xamarin HQ and dove right in. We decided to write a simple iOS app that would sync the positions of various cute monkeys between devices. Our app is written in C# and it’s called MonkeyBox. Check it out:

Arrange the monkeys to your satisfaction, scale and rotate them, and watch them sync automatically across all your devices. You can browse the MonkeyBox code by adding the Dropbox Sync component to your project, or check it out on Github.

Ok, let’s take a look at the Datastore API.

The first step is to get the user to authorize their Dropbox account. Here’s how you do that:

var manager = new DBAccountManager (“DROPBOX_APP_KEY”, “SYNC_KEY”);
var account = manager.LinkedAccount;
if (account == null)
   manager.LinkFromController (window.RootViewController); // logs in.

You can get your Dropbox App Key and Sync key from your app’s “Settings” page in the App Console.  The code above will pop up a Dropbox login view, or launch the Dropbox app if it is installed, to prompt the user to link with Dropbox. You’ll need to do some additional work to handle returning back to your app.

Ok, now we need to create a datastore. A datastore contains tables, and tables contain records. A record is a key-value pair, and values can be strings, ints, bools, or even a list of simple objects. Datastores have no schema and so each record can have an arbitrary set of value fields.

Here’s how we open the datastore:

DBError error;
var store = DBDatastore.OpenDefaultStoreForAccount (account, out error);
store.Sync (null);

Note that we called Sync() after getting a handle to the datastore. Whenever you call the Sync() method on a datastore, it will save your changes to Dropbox, and also pull any changes made on other devices. Conflicts are handled automatically using a configurable rules engine. Remember to call Sync before using a datastore to make sure you have a local copy of the data!

Now that we have access to our store, and have the data sync’d locally, we can look up values in our monkeys table:

var table = store.GetTable ("monkeys");
DBError error;
DBRecord[] results = table.Query (null, out error);
var result = results.FirstOrDefault();
var name = result.Fields ["Name"].ToString (); // e.g. ‘Bob’

And here’s how you store a new value:

DBRecord record;
records.TryGetValue (monkey.Name, out record);
record.Update (monkey.ToDictionary ());
store.Sync (null);

You can also ask Dropbox to notify you when things change:

store.AddObserver (store, () => {
  // Handle events here

That’s about it! A pretty simple API.

If you want to check out your datastore values on the web, Dropbox’s App Console now features a “Browse datastores” link in your app’s settings. It exposes a read-only view of the records in the store.

Here are some of the records from our monkey application’s datastore:


In conclusion, we think the datastore opens up some amazing new capabilities for your cross-platform apps. We’re fans, and will be eager to see how it evolves. Thanks to our partners at Dropbox for a great event and a fun new API!

Want to try this on your own? Install the Dropbox component from the Xamarin Component Store into your app in one click and start syncing.

Also at DBX, Dropbox released the Saver and Chooser SDKs (which Dropbox refers to as “Drop-ins”). Now that we’ve had our fun with the Datastore API’s, we’ll be cranking out a new Dropbox component to support Saver and Chooser. Check the Xamarin Component Store later this week. Stay tuned!

Feedback usabilla icon