5. THE OBSERVER PATTERN DONE RIGHT
REACTIVEX IS A COMBINATION OF THE BEST IDEAS
FROM
THE OBSERVER PATTERN, THE ITERATOR
PATTERN, AND FUNCTIONAL PROGRAMMING
7. - it clears up and streamlines the async
calls in your app, especially those that
might happen in diďŹerent timeframes đ
- helps with error handling during async
operations - all thrown errors are passed to
the subscription block, no more ifâs! đ
- helps with operations clarity with clear
inline notation - less line of code đ
- helps with dividing your code to smallest
possible blocks â
- allows us to forget about delegates by
using Subjects đ
- gives us plenty of easy-to-use operators
to achieve your task đ
WHYYOUSHOULDCARE
9. ⢠It will be easier for you to switch to Combine whenever Apple will update their
framework with everything RxSwift has to oďŹer (iOS 15-ish I hope đ)
⢠Rx is a standard across all major platforms (we have RxKotlin, RxJava, RxJs, etc.) - if you
learn it once, you will probably not come back and you can read code on other plaforms
easily đ
BUTALSOâŚ
11. ⢠RxSwift is hard to get used to in the
beginning since you need to rewire
your brain a bit đ§
⢠You WILL make mistakes in the
beginning, but as always, refactor is
your friend, and with smaller blocks it is
easier to fix things â
⢠... this presentation will not make you a
master Jedi. đ
BUTATTHESAMETIMEâŚ
16. LET'S THINK ABOUT A SIMPLE EXAMPLE IN OUR APP.
WHYWEDOEVENNEEDRXSWIFT?
We have a list with a Search Bar.
Whenever user is typing something to the search bar, he gets results.
Those results are mapped to ViewModels.
Then they are passed to the TableView and the View is refreshed.
SOUNDSFAMILIAR?
17. LET'S THINK ABOUT A SIMPLE EXAMPLE IN OUR APP.
WHYWEDOEVENNEEDRXSWIFT?
18. LET'S THINK ABOUT A SIMPLE EXAMPLE IN OUR APP.
WHYWEDOEVENNEEDRXSWIFT?
But if we want to modify the app so we want to:
⢠reduce number of unnecessary text inputs & API calls đą
⢠retry the call if it fails đ
⢠map response to a model that is readable by our app đ§°
⢠catch any errors that might occur đ
⢠keep our memory clear when we leave the screen đ
19. LET'S THINK ABOUT A SIMPLE EXAMPLE IN OUR APP.
WHYWEDOEVENNEEDRXSWIFT?
TEXTFIELD DELEGATES
SESSION RETRY MEMORY MANAGEMENT
DEBOUNCING
20. LET'S THINK ABOUT A SIMPLE EXAMPLE IN OUR APP.
WHYWEDOEVENNEEDRXSWIFT?
27. SUBJECTS ARE DEFINING YOUR STREAM, THEY ARE YOUR STARTING POINT.
SUBJECTS
They are a really nice way of
getting rid of any delegates.
There are four main subjects
in RxSwift world, but you will
mainly use two of them in
your projects.
PUBLISHSUBJECT BEHAVIORRELAY
28. SUBJECTS ARE DEFINING YOUR STREAM, THEY ARE YOUR STARTING POINT.
SUBJECTS
PUBLISHSUBJECT
â˘If you need to only emitnewelements to subscribers
â˘Starts empty
â˘Mostly used to notify about some changes in the app
(i.e. user gets a new message)
29. SUBJECTS ARE DEFINING YOUR STREAM, THEY ARE YOUR STARTING POINT.
SUBJECTS
PUBLISHSUBJECT
â˘Starts withinitialvalue
â˘Preserves the value
â˘Emits the value to subscribers whenever it changes
â˘Mostly used to keep some values in memory
BEHAVIORRELAY
32. THEY DEFINE TYPE OF OPERATIONS YOU WANT TO OBSERVE AND SUBSCRIBE TO.
TRAITS
OBSERVABLE SINGLE COMPLETABLE
33. OBSERVABLE
When you need to observe the change everytimesomethinghappens (i.e.
user taps a button or a number is selected)
34. SINGLE
When you only want to get a singlevalue out of the stream and then dispose
it at the end (i.e. getting resource from API).
35. COMPLETABLE
When you need to perform an operation but it doesnotreturnanythingand
youjustwaitforittocomplete (i.e. uploading something, saving something
to DB)
36. BY USING OPERATORS LIKE .FLATMAP, YOU CAN
CONVERT YOUR DATA AND USE DIFFERENT TRAITS.
45. ⢠Well, you are kind of doomed đŁ.
⢠You can use .debug() somewhere down
the stream and use a mix of
breakpoints and prints (unfortunately)
⣠in your project.
HOWTODEBUGRXSWIFT
47. ⢠You don't need to know and use all of operators and traits from RxSwift.
Some of the Rx operators are not even available in Swift. â
⢠Do not use RxSwift for everything.
Start with simple elements - basic async/API calls, then go to more
advanced examples like combining multiple observables and then
adding some side-eďŹects like loaders, error catching, etc. đ¤
RXSWIFTBESTPRACTICES
48. ⢠Do not expose a Subject outside of your service layer class (use
Observable), so nobody outside of the class can modify or publish to the
stream directly. Exposing subject as an Observable makes it a one-way
street. đŚş
⢠Use [unowned/weak self] in every closure that uses self to avoid memory
leaks âź This is the ultimate enemy of RxSwift đť
⢠If you don't want to stop your subscription after an error, use .materialize
đŞ. It will change wrap your events into Result-like structures that you
can handle in your subscription block.
⢠If your RxSwift code does not compile, write simpler code đľ. Check your
types. Swift compiler does not have a good RxSwift understanding đ¤Ź
RXSWIFTBESTPRACTICES