Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Reactive Extensions (Rx)

Reactive Extensions (Rx)

A quick overview of the concepts behind Reactive Extensions. I also briefly touch on Reactive Cocoa and show some basic examples

Ray Yamamoto Hilton

December 13, 2012
Tweet

More Decks by Ray Yamamoto Hilton

Other Decks in Technology

Transcript

  1. Imperative Programming • evaluation occurs at time of assignment •

    if the values of b or c later change, a remains unaffected Friday, 14 December 12
  2. FRP • a is /bound/ to the result of b

    + c • Consequent changes to b or c will re- evaluate b + c and assign the result to a • Formulas in spreadsheets are a good example • Obj-C KVO can be used in this way Friday, 14 December 12
  3. Rx “a library for composing asynchronous and event- based programs

    using observable collections” Friday, 14 December 12
  4. Async Events • Simplify asynchronous and event-based programming • Maximize

    concurrency but minimize explicit concurrency code • Avoid doing Too Much on the main thread • Scale across multiple cores Friday, 14 December 12
  5. Composition • Sequence a series of simple async operators •

    Combine result of multiple async events • Avoid boiler plate code, keep intent clear Friday, 14 December 12
  6. Observable Collections • Collections are async data sources that follow

    the Observer pattern • Scalar properties can thought of as a collection of values through time • .NET has LINQ, an SQL-like expression syntax for querying arbitrary data structures • filters, projections, joins and time-based operations Friday, 14 December 12
  7. Operators • Filter and transform data as it flows through

    • Synonymous to IO stream programming • Chain many operations to only get the information you want • e.g. DistinctUntilChanged, Until/WaitUntil, Throttle(timeout) Friday, 14 December 12
  8. Simple examples ! [RACAble(self.text) subscribeNext:^(id x) { ! ! NSLog(@"text:

    %@", x); ! }]; ! [[RACAble(self.text) ! ! map:^(id x) { ! ! ! return [x uppercaseString]; ! ! }] ! ! toProperty:@keypath(self.label) onObject:self]; ! [self rac_bind:@keypath(self.text) to:self.view.textField.rac_textSignal]; Friday, 14 December 12
  9. Async Image Loading ! [[[[[RACAble(self.userAccount.avatarURL) ! ! filter:^ BOOL (id

    x) { ! ! ! return x != nil; ! ! }] ! ! deliverOn:[RACScheduler scheduler]] ! ! flattenMap:^(NSURL *URL) { ! ! ! return [UIImage imageWithContentsOfURL:URL]; ! ! }] deliverOn:RACScheduler.mainThreadScheduler] subscribeNext:^(id image) { self.imageView.image = image; }]; Friday, 14 December 12
  10. Join Async Operations ! [[[RACSignal ! ! merge:@[[self fetchUser], [self

    fetchRepos]]] ! ! finally:^{ ! ! ! self.loading = NO; ! ! }] ! ! subscribeNext:^(id x) { ! ! ! NSLog(@"completed!”); ! ! } error:^(NSError *error) { ! ! ! NSLog(@"error: %@", error); ! ! } completed:^{ ! ! ! NSLog(@"done"); ! ! }]; Friday, 14 December 12
  11. Conclusions • You are probably doings some of these things

    • Formalizes an approach for async event- based functional programming • Controllers become more declarative • Bracket notation kinda sucks for chaining many calls Friday, 14 December 12