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

Intro to Reactive programming in iOS using RxSwift

Intro to Reactive programming in iOS using RxSwift

Presentation of my talk @SwiftCairo Meetup about the reactive programming
what, why and how to use it in iOS.

Attia Mo

April 24, 2019
Tweet

More Decks by Attia Mo

Other Decks in Programming

Transcript

  1. A t t i a M o Quick Tips For

    Writing RxSwift. @AttiaMoTheDev
  2. A t t i a M o @AttiaMoTheDev $ Reactive

    Programming % & ReactiveX ' ( RxSwift ) * RxSwift ⌚﹠ - Community / Resources Agenda .
  3. A t t i a M o @AttiaMoTheDev ⚫ “Data

    streams and the propagation of change”. ⚫ “Event Based Model”. ⚫ “Asnyc Sequence Of Events”.
  4. Why to use RxSwift? Benefits and use cases Synchronize two

    requests ⏬ However, despite the obvious powerful capabilities of KVO, the RXSwift creators solve this issue radically by minimizing the necessity to involve this API at all. Delegates required keeping in mind a huge amount of information, RxSwift managed to solve this problem completely. This library had introduced the ability to write code in the declarative style without increasing the size of the UIViewController and requiring the objc
 You’re bored of KVO/Delegates RP leads to writing less code. 
 Since you can react to events in a declarative way you can avoid certain complications. Like nested callbacks, delegates, notifications, etc. For example, in the previous login process.
 Since Rx streams are immutable, you can just react to changes and create objects that are open to extensions and closed to modifications.
 Rx in MVVM and other MVC improvements like VIPER. You can bind objects of different architecture layers and the architecture remains clean. Cleaner Code & Architectures. Providing the synchronization for the calls can be difficult even for an experienced developer.
 If you are using RxSwift, you can simply use a zip operator that would combine the two observables and send an answer at the end of processing two API requests. A t t i a M o @AttiaMoTheDev https://medium.com/@leandromperez/why-use-rxswift-a176b553a705
 https://applikeysolutions.com/blog/rxswift-benefits-and-use-cases
  5. Why to use RxSwift? Benefits and use cases https://medium.com/@leandromperez/why-use-rxswift-a176b553a705
 https://applikeysolutions.com/blog/rxswift-benefits-and-use-cases

    Multi-threading is simplified. The structures provide different ways of publishing information. For instance, a PublishSubject or a BehaviorSubject. They are different ways of channeling events. The operations transform, decorate, compose, etc, the information. Like map, filter, concat, etc. The key is that everyone can use the same structures and operations. No matter what they put in them. Modular Code? Reactive programming is a way of working, it’s a design pattern, or a set of them for all that matters. You can create reusable compassable components because of the standard structures and operations. The same standardization comes in different languages and platforms. So If you learn Reactive Programming in Swift, you can take it with you to another platform and language. Multi-platform? Using RxSwift, you can react to changes on different threads. You do this with a lot less code, less complexity, less bugs. You can listen to an event on the main thread and react in background. Finally you can go back to the main thread to show the results. The resulting code can be very simple: you observeOn different Schedulers. A t t i a M o @AttiaMoTheDev
  6. Observables are the heart of Rx. You’re going to spend

    some time discussing what observables are, how to create them, and how to use them. What is an observable? Observables. A t t i a M o @AttiaMoTheDev
  7. Lifecycle of an observable A t t i a M

    o @AttiaMoTheDev • An observable emits next events that contain elements. It can continue to do this until it either: • emits an error event and is terminated, or • emits a Completed event and is terminated. • Once an observable is terminated, it can no longer emit events. Observable Lifecycle
  8. A t t i a M o @AttiaMoTheDev • An

    observable emits next events that contain elements. It can continue to do this until it either: • emits an error event and is terminated, or • emits a Completed event and is terminated. • Once an observable is terminated, it can no longer emit events. Observable Lifecycle
  9. something that can act as both an observable and as

    an observer. And that something is called a Subject. What is Subjects? Subjects. A t t i a M o @AttiaMoTheDev
  10. • PublishSubject: Starts empty and only emits new elements to

    subscribers. • BehaviorSubject: Starts with an initial value and replays it or the latest element to new subscribers. • ReplaySubject: Initialized with a buffer size and will maintain a buffer of elements up to that size and replay it to new subscribers. • Variable: Wraps a BehaviorSubject, preserves its current value as state, and replays only the latest/initial value to new subscribers. Four subject types in RxSwift Subjects Types. A t t i a M o @AttiaMoTheDev
  11. Operators are the building blocks of Rx, which you can

    use to transform, process, and react to events emitted by observables. Just as you can combine simple arithmetic operators like +, -, and / to create complex math expressions, you can chain and compose together Rx's simple operators to express complex app logic. What is an Operators? Operators. A t t i a M o @AttiaMoTheDev
  12. Considering that schedulers are simply a context, which could be

    anything (a dispatch queue, thread, or a custom context), and that all operators transforming sequences need to preserve the implicit guarantees, you need to be sure you’re using the right scheduler. Schedulers. Schedulers. A t t i a M o @AttiaMoTheDev
  13. RxTest is a separate library from RxSwift. It’s hosted within

    the RxSwift repo but requires a separate pod install and import. RxTest provides many useful additions for testing RxSwift code, such as TestScheduler, which is a virtual time scheduler that gives you granular control over testing time-linear operations, and methods including next(_:_:), completed(_:_:), and error(_:_:) RxTest RxTest. A t t i a M o @AttiaMoTheDev
  14. A t t i a M o @AttiaMoTheDev Learn &

    Master ⚔ the Basics of RxSwift in 10 Minutes https://rxmarbles.com/#throttle https://academy.realm.io/posts/try-swift-nyc-2017-krunoslav-zaher-modern- rxswift-architectures/ https://www.codingame.com/playgrounds/929/reactive-programming-with- reactor-3/Intro https://github.com/mohammadZ74/MVVMRx_SampleProject
  15. A t t i a M o @AttiaMoTheDev $ Reactive

    Programming % & ReactiveX ' ( RxSwift ) * RxSwift ⌚﹠ - Community / Resources Recap .