{"id":6244,"date":"2013-07-10T19:20:51","date_gmt":"2013-07-10T23:20:51","guid":{"rendered":"http:\/\/blog.xamarin.com\/?p=6244"},"modified":"2019-03-31T19:09:44","modified_gmt":"2019-04-01T02:09:44","slug":"a-quick-look-at-dropboxs-new-datastore-api-in-c-sharp","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/a-quick-look-at-dropboxs-new-datastore-api-in-c-sharp\/","title":{"rendered":"A quick look at Dropbox\u2019s new Datastore API"},"content":{"rendered":"<p><img decoding=\"async\" class=\" wp-image-6276 alignright\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/image01.png\" alt=\"image01\" width=\"140\" height=\"210\" \/><\/p>\n<p>Dropbox released a new API for mobile app developers yesterday at their <a href=\"https:\/\/www.dropbox.com\/dbx\">DBX<\/a> conference: a <a href=\"https:\/\/www.dropbox.com\/developers\/datastore\">Datastore API<\/a> which allows you to sync data between a user\u2019s devices in real-time, via their dropbox account.<\/p>\n<p>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\u2019s called MonkeyBox. Check it out:<\/p>\n<p>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 <a href=\"https:\/\/components.xamarin.com\/view\/dropboxsync\">the Dropbox Sync component<\/a> to your project, or check it out on <a href=\"https:\/\/github.com\/Clancey\/MonkeyBox\">Github<\/a>.<\/p>\n<p dir=\"ltr\">Ok, let\u2019s take a look at the Datastore API.<\/p>\n<p dir=\"ltr\">The first step is to get the user to authorize their Dropbox account. Here\u2019s how you do that:<\/p>\n<pre class=\"lang:csharp decode:true\">var manager = new DBAccountManager (\u201cDROPBOX_APP_KEY\u201d, \u201cSYNC_KEY\u201d);\r\nvar account = manager.LinkedAccount;\r\nif (account == null)\r\n   manager.LinkFromController (window.RootViewController); \/\/ logs in.\r\n<\/pre>\n<p>You can get your Dropbox App Key and Sync key from your app\u2019s \u201cSettings\u201d page in <a href=\"https:\/\/www.dropbox.com\/developers\/apps\">the App Console<\/a>. \u00a0The 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\u2019ll need to do <a href=\"https:\/\/www.dropbox.com\/developers\/datastore\/docs\/ios#DBAccountManager.handleOpenURL:\">some additional work<\/a> to handle returning back to your app.<\/p>\n<p dir=\"ltr\">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.<\/p>\n<p dir=\"ltr\">Here\u2019s how we open the datastore:<\/p>\n<pre class=\"lang:csharp decode:true\">DBError error;\r\nvar store = DBDatastore.OpenDefaultStoreForAccount (account, out error);\r\nstore.Sync (null);\r\n<\/pre>\n<p dir=\"ltr\">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 href=\"https:\/\/www.dropbox.com\/developers\/datastore\/tutorial\/ios#conflicts\">a configurable rules engine<\/a>. Remember to call Sync before using a datastore to make sure you have a local copy of the data!<\/p>\n<p dir=\"ltr\">Now that we have access to our store, and have the data sync\u2019d locally, we can look up values in our monkeys table:<\/p>\n<pre class=\"lang:csharp decode:true\">var table = store.GetTable (\"monkeys\");\r\nDBError error;\r\nDBRecord[] results = table.Query (null, out error);\r\nvar result = results.FirstOrDefault();\r\nvar name = result.Fields [\"Name\"].ToString (); \/\/ e.g. \u2018Bob\u2019\r\n<\/pre>\n<p dir=\"ltr\">And here\u2019s how you store a new value:<\/p>\n<pre class=\"lang:csharp decode:true\">DBRecord record;\r\nrecords.TryGetValue (monkey.Name, out record);\r\nrecord.Update (monkey.ToDictionary ());\r\nstore.Sync (null);\r\n<\/pre>\n<p>You can also ask Dropbox to notify you when things change:<\/p>\n<pre class=\"lang:csharp decode:true\">store.AddObserver (store, () =&gt; {\r\n  \/\/ Handle events here\r\n});\r\n<\/pre>\n<p>That\u2019s about it! A pretty simple API.<\/p>\n<p>If you want to check out your datastore values on the web, Dropbox\u2019s <a href=\"https:\/\/www.dropbox.com\/developers\/apps\">App Console<\/a> now features a \u201cBrowse datastores\u201d link in your app\u2019s settings. It exposes a read-only view of the records in the store.<\/p>\n<p>Here are some of the records from our monkey application\u2019s datastore:<\/p>\n<p style=\"text-align: center;\"><a href=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/image00.png\"><img decoding=\"async\" class=\"alignnone size-medium wp-image-6275 aligncenter\" src=\"\/wp-content\/uploads\/sites\/44\/2019\/04\/image00.png\" alt=\"image00\" width=\"300\" height=\"195\" \/><\/a><\/p>\n<p dir=\"ltr\">In conclusion, we think the datastore opens up some amazing new capabilities for your cross-platform apps. We\u2019re fans, and will be eager to see how it evolves. Thanks to our<a href=\"\/dropbox-sync-component-and-dropbox-at-evolve\/\"> partners at Dropbox<\/a> for a great event and a fun new API!<\/p>\n<p dir=\"ltr\">Want to try this on your own? Install the <a href=\"blank\">Dropbox component<\/a> from the Xamarin Component Store into your app in one click and start syncing.<\/p>\n<p dir=\"ltr\">Also at DBX, Dropbox released <a href=\"https:\/\/www.dropbox.com\/developers\/dropins\">the Saver and Chooser SDKs<\/a> (which Dropbox refers to as \u201cDrop-ins\u201d). Now that we\u2019ve had our fun with the Datastore API\u2019s, we\u2019ll be cranking out a new Dropbox component to support Saver and Chooser. Check the Xamarin Component Store later this week. Stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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\u2019s 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 [&hellip;]<\/p>\n","protected":false},"author":579,"featured_media":39167,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4],"class_list":["post-6244","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-developers","tag-xamarin-platform"],"acf":[],"blog_post_summary":"<p>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\u2019s 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/6244","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/579"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=6244"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/6244\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/39167"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=6244"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=6244"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=6244"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}