RxSwift foundation and basic components
Reactive programming has been in the picture for more than 20 years now, but it did not really start to gain momentum until the introduction of reactive extensions in 2009. Reactive extensions or Rx are available for several languages, and they look and behave the same for the most part across all these languages. Development began on RxSwift in February 2015, and it became the official reactive extension for Swift in August the same year.
Reactive extensions
As mentioned earlier, all of the Rx extensions look and behave the same way, and that’s because each language implements the same patterns and operators in a language-specific idiomatic way.
There are also platform-specific extensions such as Cocoa and CocoaTouch, which means that you can develop in the language and platform of your choice, which will be the native Swift platform in this case. The Rx skills are universal and portable to other languages and platforms, which means that teams working with cross-platforms are able to better coordinate using reactive extensions.
Rx offers a common set of operators: To perform various tasks based on events in an asynchronous fashion, Rx typically implements two common patterns:
- A subject maintains a list of dependents called Observers, and it notifies them of changes. NotificationCenter and KVO are also implementations of this observer pattern.
- Rx also implements the iterator pattern, which is how sequences can be traversed. If you have spent time with Swift, then you will have definitely worked with the iterator pattern while trying to traverse over sequences and collections.
Observable
An observable sequence is just a sequence. The key advantage of an observable sequence over Swift’s standard sequence type is that it can also operate asynchronously.
Subject
Subject is a special type in RxSwift that can act as both of these:
- An Observable sequence, which means it can be subscribed to
- An Observer that enables adding new elements onto a subject that will then be emitted to the subject subscribers
There are four subject types in RxSwift, each with unique characteristics that you may find useful in different scenarios. They are as listed:
- PublishSubject
- BehaviorSubject
- ReplaySubject
- Variable
Marble diagrams
As discussed earlier, we know that Rx is all about working with Observable sequences; the way you work with them is by using one or more operators on them. Operators are methods of the various observable types as discussed earlier.
Marble diagrams are interactive diagrams that depict how Rx operators work with observable sequences.
Schedulers
An observable and the operators you use on it will execute and notify observers on the same thread on which the subscribe operator is being used.
Schedulers are an abstraction that let you specify distinct queues on which to perform work. It is always a good idea to move all the intensive work off the main thread to keep it as responsive as possible for users, and schedulers make it easy to do this. They can be Serial or Concurrent.
To read the complete blog please follow the link here:
To have a solid grasp over reactive concepts and write IOS applications in RxSwift you can find the link to my book titled: Reactive programming in Swift 4 here:
Thanks for reading, please share it if you found it useful :)