Let’s Bolt!

graphicWhen Parse joined Facebook, we immediately started looking for ways to improve our SDKs by comparing code and learning from each others’ successes. We found that there were a lot of small, low-level utility classes in iOS and Android that we had both implemented. Rather than continue to have two versions of these components, we decided to collaborate on one common library between our SDKs. Today, we are open-sourcing that library to make it available to others.

Bolts is a collection of low-level libraries designed to make developing mobile apps easier. Using Bolts does not require using any Parse or Facebook services, nor does it require having a Parse or Facebook developer account. Simply download the jar or framework file and drop it into your project. Or you can download the source directly from GitHub. Documentation for all of the components in Bolts is available on GitHub as well.

The first component in Bolts is “tasks”, which make organization of complex asynchronous code more manageable. A task is kind of like a JavaScript Promise, but available for iOS and Android. For example, if you have an asynchronous method for saving an object in iOS, you can have it return a BFTask*, and handle the result in a continuation block.

[[object saveAsync:obj] continueWithBlock:^id(BFTask *task) {
  if (task.isCancelled) {
    // the save was cancelled.
  } else if (task.error) {
    // the save failed.
  } else {
    // the object was saved successfully.
    SaveResult *saveResult = task.result;
  }
  return nil;
}];

The equivalent code in Android would be:

object.saveAsync().continueWith(new Continuation<ParseObject, Void>() {
  public Void then(Task task) throws Exception {
    if (task.isCancelled()) {
      // the save was cancelled.
    } else if (task.isFaulted()) {
      // the save failed.
      Exception error = task.getError();
    } else {
      // the object was saved successfully.
      SaveResult saveResult = task.getResult();
    }
    return null;
  }
});

Tasks have many advantages over the other models for asynchronous development on these platforms, such as AsyncTask and NSOperation. For more information and example code, please see the platform-specific README files on GitHub.

More Bolts will be coming soon!

Bryan Klimt
January 29, 2014
blog comments powered by Disqus

Comments are closed.

Archives

Categories

RSS Feed Follow us Like us