Slide 1

Slide 1 text

RxSwift + MVVM

Slide 2

Slide 2 text

MVVM Model-View-ViewModel • Massive Model View Controller (MVC) • Is UIViewController a view, or a controller?

Slide 3

Slide 3 text

No content

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Just kidding :)

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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..

Slide 10

Slide 10 text

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 }) }) }) }) })

Slide 11

Slide 11 text

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) })

Slide 12

Slide 12 text

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)

Slide 13

Slide 13 text

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)'") }

Slide 14

Slide 14 text

KVO view.rx_observe(CGRect.self, "frame") .subscribeNext { (frame: CGRect?) in print("Got new frame \(frame)") }

Slide 15

Slide 15 text

Units • Observable vs Driver • Driver can't error out • Observe on main scheduler • Introduced in RxSwift 2.0

Slide 16

Slide 16 text

Demo

Slide 17

Slide 17 text

RxSwift • You don't necessarily: • Have to use all of it's features • Or use functional programming for everything • Or understand monads

Slide 18

Slide 18 text

Resources • Github repo • APIs • Slack • Example apps (on Github repo)