Evernote Tech Blog

The Care and Feeding of Elephants

Reactive Cocoa and the Evernote iOS SDK

Async Chaining

The Evernote SDK for iOS has a block based api, so that developers can perform network tasks in the background. Callbacks are a nice way to do async operations but when multiple callbacks are nested, code quickly becomes difficult to read and it can become hard to figure out what the intent of the code is. Callback aggregation helps you get past this problem, and in JavaScript, Promises are gaining popularity as a solution. For Objective-C, lets look at two approaches that help with this problem.

NSOperation

The first approach is to use NSOperation. By using NSOperation dependencies, multiple operations can be aggregated. This approach can be used with the existing Evernote SDK for iOS. In the example below, we find a notebook and then create a note once the notebook is found.

[gist id=da105b629f77f7aeb157]

ReactiveCocoa

Our second approach is to use Functional Reactive Programming (FRP),  which is very useful for aggregating callbacks and centralizing error handling by chaining requests together. It also a very powerful way of expressing intent in code. ReactiveCocoa by GitHub is the open source Objective-C version of FRP. Using it will require some changes in the Evernote SDK for iOS - specifically, our API’s will need to return signals. Signals can be subscribed to and subscribers can get events. This is based on top of KVO, but has a much better API (blocks – yay!). Also, it is capable of much more, like running multiple concurrent requests.

Lets see how it can be used in the Evernote SDK for iOS using the same example. Note that this required some trivial changes in the Evernote SDK and this code is experimental. All the Evernote API’s were modified to return signals so that they can be used with ReactiveCocoaRACSubject‘s are great to add signals to existing API’s.

[gist id=b68b697cea0e0816f902]

ReactiveCocoa can also be used to add callback support to an existing function, and also to run multiple concurrent requests. This example retrieves 100 notes.

[gist id=68419a354a06e434a8cf]

Signals can also be nested to merge results from multiple operations.

FRP and the Evernote SDK for iOS

FRP can be very powerful if you use it throughout your app as a unified way to do async operations. As we saw, the Evernote SDK for iOS would need some changes to be compatible with ReactiveCocoa. At Evernote, we use Thrift to generate native API bindings, so one option would be to update the Thrift Objective-C code generator to optionally generate ReactiveCocoa-compatible code. While we don’t plan to add FRP to the Evernote SDK for iOS in the near future, we are curious to know your thoughts. As always, if you like would like to contribute to the Evernote SDK for iOS, please feel free to open a pull request.

If you are building an app using the Evernote platform, make sure you submit to Devcup. We also plan to have a meetup at Evernote for iOS developers very soon with some great speakers. Stay tuned!

Leave a Comment

* Required fields