SlideShare a Scribd company logo
RXSWIFT 👻
HOW TO AVOID THE HEADACHE AND FOCUS ON THE MOST IMPORTANT ELEMENTS OF IT.
RxSwift for Beginners - how to avoid a headache of reactive programming
WHAT THE HECK ISREACTIVE
PROGRAMMING?
REACTIVEX.IO
“AN API FOR ASYNCHRONOUS
PROGRAMMING
WITH OBSERVABLESTREAMS”
THE OBSERVER PATTERN DONE RIGHT
REACTIVEX IS A COMBINATION OF THE BEST IDEAS
FROM
THE OBSERVER PATTERN, THE ITERATOR
PATTERN, AND FUNCTIONAL PROGRAMMING
WHY YOU WOULD WANT TO HAVE
RXSWIFTINTHEPROJECT?
- it clears up and streamlines the async
calls in your app, especially those that
might happen in different 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
BUTALSO…
• It will be easier for you to switch to Combine whenever Apple will update their
framework with everything RxSwift has to offer (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…
BUTATTHESAMETIME...
• 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…
WHYWEDOEVENNEEDRXSWIFT?
NOWITMAYLOOKLIKETHIS:
WHATITCANLOOKLIKE
NEEDANEXAMPLE?
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?
LET'S THINK ABOUT A SIMPLE EXAMPLE IN OUR APP.
WHYWEDOEVENNEEDRXSWIFT?
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 😇
LET'S THINK ABOUT A SIMPLE EXAMPLE IN OUR APP.
WHYWEDOEVENNEEDRXSWIFT?
TEXTFIELD DELEGATES
SESSION RETRY MEMORY MANAGEMENT
DEBOUNCING
LET'S THINK ABOUT A SIMPLE EXAMPLE IN OUR APP.
WHYWEDOEVENNEEDRXSWIFT?
HOW TO
VISUALISE
RXSWIFT
RxSwift for Beginners - how to avoid a headache of reactive programming
SUBJECTS&TRAITS
OPERATORS
SUBSCRIPTIONS
SUBJECTS&TRAITS
OPERATORS
SUBSCRIPTIONS
DISPOSEBAG
NOTHING HAPPENS UNTIL YOU
.SUBSCRIBE
SUBJECTS
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
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)
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
HOW TO DEFINE AND USE A SUBJECT?
SUBJECTS
TRAITS
THEY DEFINE TYPE OF OPERATIONS YOU WANT TO OBSERVE AND SUBSCRIBE TO.
TRAITS
OBSERVABLE SINGLE COMPLETABLE
OBSERVABLE
When you need to observe the change everytimesomethinghappens (i.e.
user taps a button or a number is selected)
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).
COMPLETABLE
When you need to perform an operation but it doesnotreturnanythingand
youjustwaitforittocomplete (i.e. uploading something, saving something
to DB)
BY USING OPERATORS LIKE .FLATMAP, YOU CAN
CONVERT YOUR DATA AND USE DIFFERENT TRAITS.
OPERATORS
THERE ARE PLENTY OF THEM
OPERATORS
SOMEOFTHEUSEFULOPERATORS:
SOMEOFTHEUSEFULOPERATORS:
SOMEOFTHEUSEFULOPERATORS:
HOW TO MIGRATEYOUREXISTING
ASYNCFUNCTIONS TO RX
HOWTOMIGRATEYOUREXISTINGASYNCFUNCTIONSTORX
HOW TO DEBUG RXSWIFT
• 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
RXSWIFT BEST PRACTICES
• 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-effects like loaders, error catching, etc. 🤘
RXSWIFTBESTPRACTICES
• 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
http://reactivex.io/documentation/operators.html
https://medium.com/@hudnitsky/elegant-rxswift-injection-into-legacy-code-d974ad7f0d5
http://adamborek.com/creating-observable-create-just-deferred/
https://speakerdeck.com/freak4pc/rxswift-debunking-the-myth-of-hard
http://adamborek.com/memory-managment-rxswift/
https://medium.com/ios-os-x-development/learn-and-master-%EF%B8%8F-the-basics-of-rxswift-in-10-
minutes-818ea6e0a05b
https://www.raywenderlich.com/books/rxswift-reactive-programming-with-swift/v4.0/chapters/1-hello-
rxswift
USEFULLINKS
RxSwift for Beginners - how to avoid a headache of reactive programming
QUESTIONS?
RxSwift for Beginners - how to avoid a headache of reactive programming

More Related Content

PPT
Creating A Vba Function Library For Use In QTP/VBSCRIPT
PDF
Ryan Christiani I Heard React Was Good
PDF
React For Vikings
PDF
Intro to javascript (4 week)
PDF
Anatomy of a Reactive Application
PDF
DRAKON Visual Language: Tutorial. Part 2
PDF
Forward JS 2017 | SF | Write applications as State Machines
PDF
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer
Creating A Vba Function Library For Use In QTP/VBSCRIPT
Ryan Christiani I Heard React Was Good
React For Vikings
Intro to javascript (4 week)
Anatomy of a Reactive Application
DRAKON Visual Language: Tutorial. Part 2
Forward JS 2017 | SF | Write applications as State Machines
E2E testing Single Page Apps and APIs with Cucumber.js and Puppeteer

What's hot (20)

PDF
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
PDF
Angular Testing
PDF
Asynchronous javascript
PPTX
PowerShell: Automation for everyone
KEY
Ruby Testing: Cucumber and RSpec
PPTX
Async CTP 3 Presentation for MUGH 2012
PDF
Serverless in production, an experience report (LNUG)
PDF
Some experiences building an Android app with React Native & Redux
PDF
Pretenders talk at PyconUK 2012
PDF
Serverless in Production, an experience report (cloudXchange)
ODP
JQuery Conf Berlin - Ondrisek - From Java To AngularJS (without pain)
PPTX
Calabash Mobile Application Testing Overview
PDF
A Whale and an Elephant, when PHP meets docker
PDF
Async await in JavaScript
PPTX
Angular elements - embed your angular components EVERYWHERE
PDF
OpenLayers 3 & Google Closure Compiler
PDF
Using Async in your Mobile Apps - Marek Safar
PDF
SPA Flask Vue
PDF
Rick Blalock: Your Apps are Leaking - Controlling Memory Leaks
PDF
Introduction to Functional Reactive Programming
End to end testing Single Page Apps & APIs with Cucumber.js and Puppeteer (Em...
Angular Testing
Asynchronous javascript
PowerShell: Automation for everyone
Ruby Testing: Cucumber and RSpec
Async CTP 3 Presentation for MUGH 2012
Serverless in production, an experience report (LNUG)
Some experiences building an Android app with React Native & Redux
Pretenders talk at PyconUK 2012
Serverless in Production, an experience report (cloudXchange)
JQuery Conf Berlin - Ondrisek - From Java To AngularJS (without pain)
Calabash Mobile Application Testing Overview
A Whale and an Elephant, when PHP meets docker
Async await in JavaScript
Angular elements - embed your angular components EVERYWHERE
OpenLayers 3 & Google Closure Compiler
Using Async in your Mobile Apps - Marek Safar
SPA Flask Vue
Rick Blalock: Your Apps are Leaking - Controlling Memory Leaks
Introduction to Functional Reactive Programming
Ad

Similar to RxSwift for Beginners - how to avoid a headache of reactive programming (20)

PPTX
Abap for functional consultants
PDF
Ready Layer One: Intro to the Model Context Protocol
PDF
379008-rc217-functionalprogramming
PDF
Úvod do programování 7
PDF
Service worker API
PDF
Interactive workflow management using Azkaban
PDF
Ask the expert - App performance on Series 40 phones
PPTX
Guidelines to understand durable functions with .net core, c# and stateful se...
PDF
slides-students-C03.pdf
PDF
apidays LIVE Hong Kong 2021 - GraphQL : Beyond APIs, graph your enterprise by...
PDF
Javascript - Ebook (A Quick Guide)
PDF
Quo vadis, JavaScript? Devday.pl keynote
PPT
Eclipse - Single Source;Three Runtimes
PDF
Solid And Sustainable Development in Scala
PDF
Become a webdeveloper - AKAICamp Beginner #1
PDF
Smart Client Development
PDF
Maxim Salnikov - Service Worker: taking the best from the past experience for...
PDF
James Baxley - Statically typing your GraphQL app
PDF
Solid and Sustainable Development in Scala
PDF
Apache Samza 1.0 - What's New, What's Next
Abap for functional consultants
Ready Layer One: Intro to the Model Context Protocol
379008-rc217-functionalprogramming
Úvod do programování 7
Service worker API
Interactive workflow management using Azkaban
Ask the expert - App performance on Series 40 phones
Guidelines to understand durable functions with .net core, c# and stateful se...
slides-students-C03.pdf
apidays LIVE Hong Kong 2021 - GraphQL : Beyond APIs, graph your enterprise by...
Javascript - Ebook (A Quick Guide)
Quo vadis, JavaScript? Devday.pl keynote
Eclipse - Single Source;Three Runtimes
Solid And Sustainable Development in Scala
Become a webdeveloper - AKAICamp Beginner #1
Smart Client Development
Maxim Salnikov - Service Worker: taking the best from the past experience for...
James Baxley - Statically typing your GraphQL app
Solid and Sustainable Development in Scala
Apache Samza 1.0 - What's New, What's Next
Ad

More from Maciej Kołek (7)

PDF
Od U do Z - jak powinna wyglądać Twoja aplikacja na platformie iOS?
PDF
Apple Watch - Jak tworzyć aplikacje na SmartWatcha z problemami wieku dziecię...
PDF
TipiUX#4: Od pomysłu do wdrożenia - proces projektowania interfejsów aplikacj...
PDF
TouchID, Handoff, Spotlight oraz Multitasking: Nowości W Projektowaniu Interf...
PDF
Podstawy Wordpressa
PDF
Z Perspektywy Programisty: Projektowanie Interfejsów Aplikacji Mobilnych
PDF
Tworzenie aplikacji na platformę watchOS2
Od U do Z - jak powinna wyglądać Twoja aplikacja na platformie iOS?
Apple Watch - Jak tworzyć aplikacje na SmartWatcha z problemami wieku dziecię...
TipiUX#4: Od pomysłu do wdrożenia - proces projektowania interfejsów aplikacj...
TouchID, Handoff, Spotlight oraz Multitasking: Nowości W Projektowaniu Interf...
Podstawy Wordpressa
Z Perspektywy Programisty: Projektowanie Interfejsów Aplikacji Mobilnych
Tworzenie aplikacji na platformę watchOS2

Recently uploaded (6)

DOC
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
PPTX
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
PDF
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
PDF
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
DOC
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
PPTX
ASMS Telecommunication company Profile
Camb毕业证学历认证,格罗斯泰斯特主教大学毕业证仿冒文凭毕业证
Introduction to Packet Tracer Course Overview - Aug 21 (1).pptx
Lesson 13- HEREDITY _ pedSAWEREGFVCXZDSASEWFigree.pdf
6-UseCfgfhgfhgfhgfhgfhfhhaseActivity.pdf
证书学历UoA毕业证,澳大利亚中汇学院毕业证国外大学毕业证
ASMS Telecommunication company Profile

RxSwift for Beginners - how to avoid a headache of reactive programming