Swarm Documentation API Reference

Adding Cloud Data to your App

Estimated Integration Time: 5 minutes

Cloud Data provides an easy way to save and load data across multiple devices and installs. Think of Cloud Data as an online version of Android's SharedPreferences, where you can set and get data, and have it saved to the cloud in a seamless way. Cloud Data can be used to store level progress, configuration options, favorites, bookmarks, or anything else you might need.

Prerequisites:


Store Cloud Data
What data you store in the cloud is completely up to you. All data is stored as a String, and it is delivered back to your application as a String (max length: 2048 chars). In the example here, we will be saving and loading the user's progress through a game. The user's progress will be measured by storing the number of the last level they've completed.
// Save our level progress to the cloud (probably right after completing a level)
if (Swarm.isLoggedIn()) {
    Swarm.user.saveCloudData("levelProgress", ""+levelProgress);
}
The user's progress has been saved to the cloud, and it will now be available on any device they log into in the future. The data is also cached locally, so if the user's network connection is interrupted, Cloud Data will still function properly.

Swarm automatically caches the Cloud Data efficiently, because a user may not always have a reliable connection to the internet. If the user is offline, cached versions of the data will be returned during get requests, and updated on both the server (and in the cache) on set requests. However, it is possible for a request to fail. If the user is offline and the data has never been requested when the user was online, Cloud Data get requests can return null.

If data for a given key has never been set, the response will be an empty String ("") instead of null.


Retrieve Cloud Data
To get Cloud Data, start by defining a callback for when the request finishes. The code snippet below shows a callback that can be used to get the user's levelProgress data from the cloud. For more information on Swarm Callbacks, please read: callbacks.
GotCloudDataCB callback = new GotCloudDataCB() {
    public void gotData(String data) {

        // Did our request fail (network offline, and uncached)?
        if (data == null) {

            // Handle failure case.
            return;
        }

        // Has this key never been set?  Default it to a value...
        if (data.length() == 0) {
        
            // In this case, we're storing levelProgress, default them to level 1.
            data = "1";
        }

        // Parse the level data for later use
        MyGame.levelProgress = Integer.parseInt(data);
    }
}; 
Next, make an asynchronous request to Swarm for the Cloud Data.
// Make sure a user is logged in...
if (Swarm.isLoggedIn()) {
    Swarm.user.getCloudData("levelProgress", callback);
} 

Congratulations!

At this point your app should have a working version of Cloud Data. Behold the power of the cloud!