Using the new local datastores feature

The Datastore API 3.0 release includes a new preview feature called “local datastores.” Since we first released the Datastore API, developers have asked us how to deal with users who don’t have a Dropbox account. With local datastores, the same code can work regardless of whether the user has logged in with Dropbox. If a user hasn’t logged in with Dropbox, all data will just be stored locally. Once a user has logged in with a Dropbox account, data will be stored in the cloud and synced cross-device.

The Tasks example app that ships with Datastore API 3.0 shows a typical flow for using local datastores. When someone first starts using the app, the app stores everything in a local datastore. When the user decides they want to back up their data to Dropbox or sync it across devices, they can link a Dropbox account. At this point, the app will migrate the local datastore to that user’s account.

Sample code

The following sample code has been adapted from the code in the Tasks sample that ships with the iOS and Android Datastore SDKs. It shows how to switch from using a local datastore to using a linked Dropbox account:

Android version

// If the user has linked a Dropbox account for the first time...
if (justLinked) {
    // Perform a one-time migration to move from the local datastore to a remote one.
    mDatastoreManager = mLocalManager.migrateToAccount(mDbxAcctMgr.getLinkedAccount());
    mLocalManager = null;
    mDatastore = mDatastoreManager.openDefaultDatastore();
}
if (null == mDatastoreManager) {
    // If there's a linked account, use that.
    if (mDbxAcctMgr.hasLinkedAccount()) {
        mDatastoreManager = DbxDatastoreManager.forAccount(mDbxAcctMgr.getLinkedAccount());
    }
    // Otherwise, use a local datastore.
    else {
        mDatastoreManager = DbxDatastoreManager.localManager(mDbxAcctMgr);
    }
}

iOS version

// If the user has linked a Dropbox account for the first time...
if (_justLinked) {
    if (_localDatastoreManager && self.account) {
        // Perform a one-time migration to move from the local datastore to a remote one.
        [_localDatastoreManager migrateToAccount:self.account error:nil];
        _localDatastoreManager = nil;
    }
}
if (!_store) {
    // If there's a linked account, use that.
    if ([[DBAccountManager sharedManager] linkedAccount]) {
        _store = [DBDatastore openDefaultStoreForAccount:self.account error:nil];
    // Otherwise, use a local datastore.
    } else {
        _store = [DBDatastore openDefaultLocalStoreForAccountManager:[DBAccountManager sharedManager]
                                                               error:nil];
    }
}

Not for production use!

Please give local datastores a try and let us know what you think on the developer forum, but keep in mind that this feature is only in preview. Be sure to read the documentation for known issues and stay tuned for a production-ready release.