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

RxSwift+MVVM

alienxp03
January 18, 2016

 RxSwift+MVVM

Just a proof of concept/demo about RxSwift with MVVM architecture

alienxp03

January 18, 2016
Tweet

More Decks by alienxp03

Other Decks in Programming

Transcript

  1. Reactive Programming "In computing, reactive programming is a programming paradigm

    oriented around data flows and the propagation of change. This means that it should be possible to express static or dynamic data flows with ease in the programming languages used, and that the underlying execution model will automatically propagate changes through the data flow. For example, in an imperative programming setting, would mean that is being assigned the result of in the instant the expression is evaluated, and later, the values of and can be changed with no effect on the value of . However, in reactive programming, the value of would be automatically updated based on the new values, the opposite of functional programming. A modern spreadsheet program is an example of reactive programming. Spreadsheet cells can contain literal values, or formulas such as "=B1+C1" that are evaluated based on other cells. Whenever the value of the other cells change, the value of the formula is automatically updated. Another example is a hardware description language such as Verilog. In this case, reactive programming allows changes to be modeled as they propagate through a circuit. Reactive programming has foremost been proposed as a way to simplify the creation of interactive user interfaces, animations in real time systems, but is essentially a general programming paradigm. For example, in a Model-view-controller architecture, reactive programming can allow changes in the underlying model to automatically be reflected in the view, and vice versa.[1]" Wikipedia
  2. Functional Programming In computer science, functional programming is a programming

    paradigm—a style of building the structure and elements of computer programs—that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) each time. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming. Functional programming has its roots in lambda calculus, a formal system developed in the 1930s to investigate computability, the Entscheidungsproblem, function definition, function application, and recursion. Many functional programming languages can be viewed as elaborations on the lambda calculus. Another well-known declarative programming paradigm, logic programming, is based on relations.[1] Wikipedia
  3. Functional Reactive Programming (FRP) • Functional programming (avoid changing state

    and mutable data) • Reactive programming ( asynchronous dataflow ) • FP + RP = FRP • TL;DR : It's pretty awesome
  4. ReactiveCocoa • Started off with ReactiveCocoa • Changed the way

    I think and program • Currently release 4.0 RC1 • Lots of confusion for beginners like me, since lots of tutorials are using RAC 3.0 • It was good, but I couldn't really get very far because of that
  5. RxSwift • Heard about it, but was a bit skeptical

    because Rx is somewhat related to Microsoft.. • Meditated, took a deep breath, and `carthage update` • Oh how wrong can a man be..
  6. RxSwift object.doOneThing(true, completionHandler: { success in object.doAnotherThing(success, completionHandler: { success

    in object.doAnotherThing(success, completionHandler: { success in object.doAnotherThing(success, completionHandler: { success in object.doAnotherThing(success, completionHandler: { success in // Finally }) }) }) }) })
  7. RxSwift object.doOneThing(true) .flatMapLatest({ success in return object.doOneThing(success) }) .flatMapLatest({ success

    in return object.doOneThing(success) }) .flatMapLatest({ success in return object.doOneThing(success) }) .flatMapLatest({ success in return object.doOneThing(success) }) .flatMapLatest({ success in return object.doOneThing(success) })
  8. RxSwift // Conceptual solution taken from github.com/RxSwift let imageSubscription =

    imageURLs .throttle(0.2, scheduler: MainScheduler.instance) .flatMapLatest { imageURL in API.fetchImage(imageURL) } .observeOn(operationScheduler) .map { imageData in return decodeAndBlurImage(imageData) } .observeOn(MainScheduler.instance) .subscribeNext { blurredImage in imageView.image = blurredImage } .addDisposableTo(reuseDisposeBag)
  9. Delegates • You can observe a delegate rx_delegate.observe("searchBar:textDidChange:") • And

    listen for the callback searchBar.rx_text .subscribeNext { searchText in print("Current search text '\(searchText)'") }
  10. Units • Observable<E> vs Driver<E> • Driver can't error out

    • Observe on main scheduler • Introduced in RxSwift 2.0
  11. RxSwift • You don't necessarily: • Have to use all

    of it's features • Or use functional programming for everything • Or understand monads