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.
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 ReactiveCocoa. RACSubject‘s are great to add signals to existing API’s.
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.
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!