This project tries to be consistent with ReactiveX.io. The general cross platform documentation and tutorials should also be valid in case of RxSwift
.
- Observables aka Sequences
- Disposing
- Implicit
Observable
guarantees - Creating your first
Observable
(aka observable sequence) - Creating an
Observable
that performs work - Sharing subscription and
share
operator - Operators
- Playgrounds
- Custom operators
- Error handling
- Debugging Compile Errors
- Debugging
- Enabling Debug Mode
- Debugging memory leaks
- KVO
- UI layer tips
- Making HTTP requests
- RxDataSources
- Driver
- Traits: Driver, Single, Maybe, Completable
- Examples
The equivalence of observer pattern (Observable<Element>
sequence) and normal sequences (Sequence
) is the most important thing to understand about Rx.
Every Observable
sequence is just a sequence. The key advantage for an Observable
vs Swift's Sequence
is that it can also receive elements asynchronously. This is the kernel of RxSwift, documentation from here is about ways that we expand on that idea.
Observable
(ObservableType
) is equivalent toSequence
ObservableType.subscribe
method is equivalent toSequence.makeIterator
method.- Observer (callback) needs to be passed to
ObservableType.subscribe
method to receive sequence elements instead of callingnext()
on the returned iterator.
Sequences are a simple, familiar concept that is easy to visualize.
People are creatures with huge visual cortexes. When we can visualize a concept easily, it's a lot easier to reason about it.
We can lift a lot of the cognitive load from trying to simulate event state machines inside every Rx operator onto high level operations over sequences.
If we don't use Rx but model asynchronous systems, that probably means our code is full of state machines and transient states that we need to simulate instead of abstracting away.
Lists and sequences are probably one of the first concepts mathematicians and programmers learn.
Here is a sequence of numbers:
--1--2--3--4--5--6--| // terminates normally
Another sequence, with characters:
--a--b--a--a--a---d---X // terminates with error
Some sequences are finite while others are infinite, like a sequence of button taps:
---tap-tap-------tap--->
These are called marble diagrams. There are more marble diagrams at rxmarbles.com.
If we were to specify sequence grammar as a regular expression it would look like:
next* (error | completed)?
This describes the following:
- Sequences can have 0 or more elements.
- Once an
error
orcompleted
event is received, the sequence cannot produce any other element.
Sequences in Rx are described by a push interface (aka callback).
enum Event<Element> {
case next(Element) // next element of a sequence
case error(Swift.Error) // sequence failed with error
case completed // sequence terminated successfully
}
class Observable<Element> {
func subscribe(_ observer: Observer<Element>) -> Disposable
}
protocol ObserverType {
func on(_ event: Event<Element>)
}
When a sequence sends the completed
or error
event all internal resources that compute sequence elements will be freed.
To cancel production of sequence elements and free resources immediately, call dispose
on the returned subscription.
If a sequence terminates in finite time, not calling dispose
or not using disposed(by: disposeBag)
won't cause any permanent resource leaks. However, those resources will be used until the sequence completes, either by finishing production of elements or returning an error.
If a sequence does not terminate on its own, such as with a series of button taps, resources will be allocated permanently unless dispose
is called manually, automatically inside of a disposeBag
, with the takeUntil
operator, or in some other way.
Using dispose bags or takeUntil
operator is a robust way of making sure resources are cleaned up. We recommend using them in production even if the sequences will terminate in finite time.
If you are curious why Swift.Error
isn't generic, you can find the explanation here.
There is one additional way an observed sequence can terminate. When we are done with a sequence and we want to release all of the resources allocated to compute the upcoming elements, we can call dispose
on a subscription.
Here is an example with the interval
operator.
let scheduler = SerialDispatchQueueScheduler(qos: .default)
let subscription = Observable<Int>.interval(0.3, scheduler: scheduler)
.subscribe { event in
print(event)
}
Thread.sleep(forTimeInterval: 2.0)
subscription.dispose()
This will print:
0
1
2
3
4
5
Note that you usually do not want to manually call dispose
; this is only an educational example. Calling dispose manually is usually a bad code smell. There are better ways to dispose of subscriptions such as DisposeBag
, the takeUntil
operator, or some other mechanism.
So can this code print something after the dispose
call is executed? The answer is: it depends.
-
If the
scheduler
is a serial scheduler (ex.MainScheduler
) anddispose
is called on the same serial scheduler, the answer is no. -
Otherwise it is yes.
You can find out more about schedulers here.
You simply have two processes happening in parallel.
- one is producing elements
- the other is disposing of the subscription
The question "Can something be printed after?" does not even make sense in the case that those processes are on different schedulers.
A few more examples just to be sure (observeOn
is explained here).
In case we have something like:
let subscription = Observable<Int>.interval(0.3, scheduler: scheduler)
.observeOn(MainScheduler.instance)
.subscribe { event in
print(event)
}
// ....
subscription.dispose() // called from main thread
After the dispose
call returns, nothing will be printed. That is guaranteed.
Also, in this case:
let subscription = Observable<Int>.interval(0.3, scheduler: scheduler)
.observeOn(serialScheduler)
.subscribe { event in
print(event)
}
// ...
subscription.dispose() // executing on same `serialScheduler`
After the dispose
call returns, nothing will be printed. That is guaranteed.
Dispose bags are used to return ARC like behavior to RX.
When a DisposeBag
is deallocated, it will call dispose
on each of the added disposables.
It does not have a dispose
method and therefore does not allow calling explicit dispose on purpose. If immediate cleanup is required, we can just create a new bag.
self.disposeBag = DisposeBag()
This will clear old references and cause disposal of resources.
If that explicit manual disposal is still wanted, use CompositeDisposable
. It has the wanted behavior but once that dispose
method is called, it will immediately dispose any newly added disposable.
Additional way to automatically dispose subscription on dealloc is to use takeUntil
operator.
sequence
.takeUntil(self.rx.deallocated)
.subscribe {
print($0)
}
There is also a couple of additional guarantees that all sequence producers (Observable
s) must honor.
It doesn't matter on which thread they produce elements, but if they generate one element and send it to the observer observer.on(.next(nextElement))
, they can't send next element until observer.on
method has finished execution.
Producers also cannot send terminating .completed
or .error
in case .next
event hasn't finished.
In short, consider this example:
someObservable
.subscribe { (e: Event<Element>) in
print("Event processing started")
// processing
print("Event processing ended")
}
This will always print:
Event processing started
Event processing ended
Event processing started
Event processing ended
Event processing started
Event processing ended
It can never print:
Event processing started
Event processing started
Event processing ended
Event processing ended
There is one crucial thing to understand about observables.
When an observable is created, it doesn't perform any work simply because it has been created.
It is true that Observable
can generate elements in many ways. Some of them cause side effects and some of them tap into existing running processes like tapping into mouse events, etc.
However, if you just call a method that returns an Observable
, no sequence generation is performed and there are no side effects. Observable
just defines how the sequence is generated and what parameters are used for element generation. Sequence generation starts when subscribe
method is called.
E.g. Let's say you have a method with similar prototype:
func searchWikipedia(searchTerm: String) -> Observable<Results> {}
let searchForMe = searchWikipedia("me")
// no requests are performed, no work is being done, no URL requests were fired
let cancel = searchForMe
// sequence generation starts now, URL requests are fired
.subscribe(onNext: { results in
print(results)
})
There are a lot of ways to create your own Observable
sequence. The easiest way is probably to use the create
function.
Let's write a function that creates a sequence which returns one element upon subscription. That function is called 'just'.
This is the actual implementation
func myJust<E>(_ element: E) -> Observable<E> {
return Observable.create { observer in
observer.on(.next(element))
observer.on(.completed)
return Disposables.create()
}
}
myJust(0)
.subscribe(onNext: { n in
print(n)
})
This will print:
0
Not bad. So what is the create
function?
It's just a convenience method that enables you to easily implement subscribe
method using Swift closures. Like subscribe
method it takes one argument, observer
, and returns disposable.
Sequence implemented this way is actually synchronous. It will generate elements and terminate before subscribe
call returns disposable representing subscription. Because of that it doesn't really matter what disposable it returns, process of generating elements can't be interrupted.
When generating synchronous sequences, the usual disposable to return is singleton instance of NopDisposable
.
Lets now create an observable that returns elements from an array.
This is the actual implementation
func myFrom<E>(_ sequence: [E]) -> Observable<E> {
return Observable.create { observer in
for element in sequence {
observer.on(.next(element))
}
observer.on(.completed)
return Disposables.create()
}
}
let stringCounter = myFrom(["first", "second"])
print("Started ----")
// first time
stringCounter
.subscribe(onNext: { n in
print(n)
})
print("----")
// again
stringCounter
.subscribe(onNext: { n in
print(n)
})
print("Ended ----")
This will print:
Started ----
first
second
----
first
second
Ended ----
Ok, now something more interesting. Let's create that interval
operator that was used in previous examples.
This is equivalent of actual implementation for dispatch queue schedulers
func myInterval(_ interval: TimeInterval) -> Observable<Int> {
return Observable.create { observer in
print("Subscribed")
let timer = DispatchSource.makeTimerSource(queue: DispatchQueue.global())
timer.scheduleRepeating(deadline: DispatchTime.now() + interval, interval: interval)
let cancel = Disposables.create {
print("Disposed")
timer.cancel()
}
var next = 0
timer.setEventHandler {
if cancel.isDisposed {
return
}
observer.on(.next(next))
next += 1
}
timer.resume()
return cancel
}
}
let counter = myInterval(0.1)
print("Started ----")
let subscription = counter
.subscribe(onNext: { n in
print(n)
})
Thread.sleep(forTimeInterval: 0.5)
subscription.dispose()
print("Ended ----")
This will print
Started ----
Subscribed
0
1
2
3
4
Disposed
Ended ----
What if you would write
let counter = myInterval(0.1)
print("Started ----")
let subscription1 = counter
.subscribe(onNext: { n in
print("First \(n)")
})
let subscription2 = counter
.subscribe(onNext: { n in
print("Second \(n)")
})
Thread.sleep(forTimeInterval: 0.5)
subscription1.dispose()
Thread.sleep(forTimeInterval: 0.5)
subscription2.dispose()
print("Ended ----")
This would print:
Started ----
Subscribed
Subscribed
First 0
Second 0
First 1
Second 1
First 2
Second 2
First 3
Second 3
First 4
Second 4
Disposed
Second 5
Second 6
Second 7
Second 8
Second 9
Disposed
Ended ----
Every subscriber upon subscription usually generates it's own separate sequence of elements. Operators are stateless by default. There are vastly more stateless operators than stateful ones.
But what if you want that multiple observers share events (elements) from only one subscription?
There are two things that need to be defined.
- How to handle past elements that have been received before the new subscriber was interested in observing them (replay latest only, replay all, replay last n)
- How to decide when to fire that shared subscription (refCount, manual or some other algorithm)
The usual choice is a combination of replay(1).refCount()
, aka share(replay: 1)
.
let counter = myInterval(0.1)
.share(replay: 1)
print("Started ----")
let subscription1 = counter
.subscribe(onNext: { n in
print("First \(n)")
})
let subscription2 = counter
.subscribe(onNext: { n in
print("Second \(n)")
})
Thread.sleep(forTimeInterval: 0.5)
subscription1.dispose()
Thread.sleep(forTimeInterval: 0.5)
subscription2.dispose()
print("Ended ----")
This will print
Started ----
Subscribed
First 0
Second 0
First 1
Second 1
First 2
Second 2
First 3
Second 3
First 4
Second 4
First 5
Second 5
Second 6
Second 7
Second 8
Second 9
Disposed
Ended ----
Notice how now there is only one Subscribed
and Disposed
event.
Behavior for URL observables is equivalent.
This is how HTTP requests are wrapped in Rx. It's pretty much the same pattern like the interval
operator.
extension Reactive where Base: URLSession {
public func response(_ request: URLRequest) -> Observable<(Data, HTTPURLResponse)> {
return Observable.create { observer in
let task = self.dataTaskWithRequest(request) { (data, response, error) in
guard let response = response, let data = data else {
observer.on(.error(error ?? RxCocoaURLError.Unknown))
return
}
guard let httpResponse = response as? HTTPURLResponse else {
observer.on(.error(RxCocoaURLError.nonHTTPResponse(response: response)))
return
}
observer.on(.next(data, httpResponse))
observer.on(.completed)
}
task.resume()
return Disposables.create {
task.cancel()
}
}
}
}
There are numerous operators implemented in RxSwift.
Marble diagrams for all operators can be found on ReactiveX.io
Almost all operators are demonstrated in Playgrounds.
To use playgrounds please open Rx.xcworkspace
, build RxSwift-macOS
scheme and then open playgrounds in Rx.xcworkspace
tree view.
In case you need an operator, and don't know how to find it there is a decision tree of operators.
There are two ways how you can create custom operators.
All of the internal code uses highly optimized versions of operators, so they aren't the best tutorial material. That's why it's highly encouraged to use standard operators.
Fortunately there is an easier way to create operators. Creating new operators is actually all about creating observables, and previous chapter already describes how to do that.
Lets see how an unoptimized map operator can be implemented.
extension ObservableType {
func myMap<R>(transform: @escaping (E) -> R) -> Observable<R> {
return Observable.create { observer in
let subscription = self.subscribe { e in
switch e {
case .next(let value):
let result = transform(value)
observer.on(.next(result))
case .error(let error):
observer.on(.error(error))
case .completed:
observer.on(.completed)
}
}
return subscription
}
}
}
So now you can use your own map:
let subscription = myInterval(0.1)
.myMap { e in
return "This is simply \(e)"
}
.subscribe(onNext: { n in
print(n)
})
This will print:
Subscribed
This is simply 0
This is simply 1
This is simply 2
This is simply 3
This is simply 4
This is simply 5
This is simply 6
This is simply 7
This is simply 8
...
So what if it's just too hard to solve some cases with custom operators? You can exit the Rx monad, perform actions in imperative world, and then tunnel results to Rx again using Subject
s.
This isn't something that should be practiced often, and is a bad code smell, but you can do it.
let magicBeings: Observable<MagicBeing> = summonFromMiddleEarth()
magicBeings
.subscribe(onNext: { being in // exit the Rx monad
self.doSomeStateMagic(being)
})
.disposed(by: disposeBag)
//
// Mess
//
let kitten = globalParty( // calculate something in messy world
being,
UIApplication.delegate.dataSomething.attendees
)
kittens.on(.next(kitten)) // send result back to rx
//
// Another mess
//
let kittens = BehaviorRelay(value: firstKitten) // again back in Rx monad
kittens.asObservable()
.map { kitten in
return kitten.purr()
}
// ....
Every time you do this, somebody will probably write this code somewhere:
kittens
.subscribe(onNext: { kitten in
// do something with kitten
})
.disposed(by: disposeBag)
So please try not to do this.
If you are unsure how exactly some of the operators work, playgrounds contain almost all of the operators already prepared with small examples that illustrate their behavior.
To use playgrounds please open Rx.xcworkspace, build RxSwift-macOS scheme and then open playgrounds in Rx.xcworkspace tree view.
To view the results of the examples in the playgrounds, please open the Assistant Editor
. You can open Assistant Editor
by clicking on View > Assistant Editor > Show Assistant Editor
There are two error mechanisms.
Error handling is pretty straightforward. If one sequence terminates with error, then all of the dependent sequences will terminate with error. It's usual short circuit logic.
You can recover from failure of observable by using catch
operator. There are various overloads that enable you to specify recovery in great detail.
There is also retry
operator that enables retries in case of errored sequence.
When writing elegant RxSwift/RxCocoa code, you are probably relying heavily on compiler to deduce types of Observable
s. This is one of the reasons why Swift is awesome, but it can also be frustrating sometimes.
images = word
.filter { $0.containsString("important") }
.flatMap { word in
return self.api.loadFlickrFeed("karate")
.catchError { error in
return just(JSON(1))
}
}
If compiler reports that there is an error somewhere in this expression, I would suggest first annotating return types.
images = word
.filter { s -> Bool in s.containsString("important") }
.flatMap { word -> Observable<JSON> in
return self.api.loadFlickrFeed("karate")
.catchError { error -> Observable<JSON> in
return just(JSON(1))
}
}
If that doesn't work, you can continue adding more type annotations until you've localized the error.
images = word
.filter { (s: String) -> Bool in s.containsString("important") }
.flatMap { (word: String) -> Observable<JSON> in
return self.api.loadFlickrFeed("karate")
.catchError { (error: Error) -> Observable<JSON> in
return just(JSON(1))
}
}
I would suggest first annotating return types and arguments of closures.
Usually after you have fixed the error, you can remove the type annotations to clean up your code again.
Using debugger alone is useful, but usually using debug
operator will be more efficient. debug
operator will print out all events to standard output and you can add also label those events.
debug
acts like a probe. Here is an example of using it:
let subscription = myInterval(0.1)
.debug("my probe")
.map { e in
return "This is simply \(e)"
}
.subscribe(onNext: { n in
print(n)
})
Thread.sleepForTimeInterval(0.5)
subscription.dispose()
will print
[my probe] subscribed
Subscribed
[my probe] -> Event next(Box(0))
This is simply 0
[my probe] -> Event next(Box(1))
This is simply 1
[my probe] -> Event next(Box(2))
This is simply 2
[my probe] -> Event next(Box(3))
This is simply 3
[my probe] -> Event next(Box(4))
This is simply 4
[my probe] dispose
Disposed
You can also easily create your version of the debug
operator.
extension ObservableType {
public func myDebug(identifier: String) -> Observable<Self.E> {
return Observable.create { observer in
print("subscribed \(identifier)")
let subscription = self.subscribe { e in
print("event \(identifier) \(e)")
switch e {
case .next(let value):
observer.on(.next(value))
case .error(let error):
observer.on(.error(error))
case .completed:
observer.on(.completed)
}
}
return Disposables.create {
print("disposing \(identifier)")
subscription.dispose()
}
}
}
}
In order to Debug memory leaks using RxSwift.Resources
or Log all HTTP requests automatically, you have to enable Debug Mode.
In order to enable debug mode, a TRACE_RESOURCES
flag must be added to the RxSwift target build settings, under Other Swift Flags.
For further discussion and instructions on how to set the TRACE_RESOURCES
flag for Cocoapods & Carthage, see #378
In debug mode Rx tracks all allocated resources in a global variable Resources.total
.
In case you want to have some resource leak detection logic, the simplest method is just printing out RxSwift.Resources.total
periodically to output.
/* add somewhere in
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil)
*/
_ = Observable<Int>.interval(1, scheduler: MainScheduler.instance)
.subscribe(onNext: { _ in
print("Resource count \(RxSwift.Resources.total)")
})
Most efficient way to test for memory leaks is:
- navigate to your screen and use it
- navigate back
- observe initial resource count
- navigate second time to your screen and use it
- navigate back
- observe final resource count
In case there is a difference in resource count between initial and final resource counts, there might be a memory leak somewhere.
The reason why 2 navigations are suggested is because first navigation forces loading of lazy resources.
KVO is an Objective-C mechanism. That means that it wasn't built with type safety in mind. This project tries to solve some of the problems.
There are two built in ways this library supports KVO.
// KVO
extension Reactive where Base: NSObject {
public func observe<E>(type: E.Type, _ keyPath: String, options: KeyValueObservingOptions, retainSelf: Bool = true) -> Observable<E?> {}
}
#if !DISABLE_SWIZZLING
// KVO
extension Reactive where Base: NSObject {
public func observeWeakly<E>(type: E.Type, _ keyPath: String, options: KeyValueObservingOptions) -> Observable<E?> {}
}
#endif
Example how to observe frame of UIView
.
WARNING: UIKit isn't KVO compliant, but this will work.
view
.rx.observe(CGRect.self, "frame")
.subscribe(onNext: { frame in
...
})
or
view
.rx.observeWeakly(CGRect.self, "frame")
.subscribe(onNext: { frame in
...
})
rx.observe
is more performant because it's just a simple wrapper around KVO mechanism, but it has more limited usage scenarios
- it can be used to observe paths starting from
self
or from ancestors in ownership graph (retainSelf = false
) - it can be used to observe paths starting from descendants in ownership graph (
retainSelf = true
) - the paths have to consist only of
strong
properties, otherwise you are risking crashing the system by not unregistering KVO observer before dealloc.
E.g.
self.rx.observe(CGRect.self, "view.frame", retainSelf: false)
rx.observeWeakly
has somewhat slower than rx.observe
because it has to handle object deallocation in case of weak references.
It can be used in all cases where rx.observe
can be used and additionally
- because it won't retain observed target, it can be used to observe arbitrary object graph whose ownership relation is unknown
- it can be used to observe
weak
properties
E.g.
someSuspiciousViewController.rx.observeWeakly(Bool.self, "behavingOk")
KVO is an Objective-C mechanism so it relies heavily on NSValue
.
RxCocoa has built in support for KVO observing of CGRect
, CGSize
and CGPoint
structs.
When observing some other structures it is necessary to extract those structures from NSValue
manually.
Here are examples how to extend KVO observing mechanism and rx.observe*
methods for other structs by implementing KVORepresentable
protocol.
There are certain things that your Observable
s need to satisfy in the UI layer when binding to UIKit controls.
Observable
s need to send values on MainScheduler
(UIThread). That's just a normal UIKit/Cocoa requirement.
It is usually a good idea for your APIs to return results on MainScheduler
. In case you try to bind something to UI from background thread, in Debug build RxCocoa will usually throw an exception to inform you of that.
To fix this you need to add observeOn(MainScheduler.instance)
.
URLSession extensions don't return result on MainScheduler
by default.
You can't bind failure to UIKit controls because that is undefined behavior.
If you don't know if Observable
can fail, you can ensure it can't fail using catchErrorJustReturn(valueThatIsReturnedWhenErrorHappens)
, but after an error happens the underlying sequence will still complete.
If the wanted behavior is for underlying sequence to continue producing elements, some version of retry
operator is needed.
You usually want to share subscription in the UI layer. You don't want to make separate HTTP calls to bind the same data to multiple UI elements.
Let's say you have something like this:
let searchResults = searchText
.throttle(0.3, $.mainScheduler)
.distinctUntilChanged
.flatMapLatest { query in
API.getSearchResults(query)
.retry(3)
.startWith([]) // clears results on new search term
.catchErrorJustReturn([])
}
.share(replay: 1) // <- notice the `share` operator
What you usually want is to share search results once calculated. That is what share
means.
It is usually a good rule of thumb in the UI layer to add share
at the end of transformation chain because you really want to share calculated results. You don't want to fire separate HTTP connections when binding searchResults
to multiple UI elements.
Also take a look at Driver
unit. It is designed to transparently wrap those share
calls, make sure elements are observed on main UI thread and that no error can be bound to UI.
Making http requests is one of the first things people try.
You first need to build URLRequest
object that represents the work that needs to be done.
Request determines is it a GET request, or a POST request, what is the request body, query parameters ...
This is how you can create a simple GET request
let req = URLRequest(url: URL(string: "http://en.wikipedia.org/w/api.php?action=parse&page=Pizza&format=json"))
If you want to just execute that request outside of composition with other observables, this is what needs to be done.
let responseJSON = URLSession.shared.rx.json(request: req)
// no requests will be performed up to this point
// `responseJSON` is just a description how to fetch the response
let cancelRequest = responseJSON
// this will fire the request
.subscribe(onNext: { json in
print(json)
})
Thread.sleep(forTimeInterval: 3.0)
// if you want to cancel request after 3 seconds have passed just call
cancelRequest.dispose()
URLSession extensions don't return result on MainScheduler
by default.
In case you want a more low level access to response, you can use:
URLSession.shared.rx.response(myURLRequest)
.debug("my request") // this will print out information to console
.flatMap { (data: NSData, response: URLResponse) -> Observable<String> in
if let response = response as? HTTPURLResponse {
if 200 ..< 300 ~= response.statusCode {
return just(transform(data))
}
else {
return Observable.error(yourNSError)
}
}
else {
rxFatalError("response = nil")
return Observable.error(yourNSError)
}
}
.subscribe { event in
print(event) // if error happened, this will also print out error to console
}
In debug mode RxCocoa will log all HTTP request to console by default. In case you want to change that behavior, please set Logging.URLRequests
filter.
// read your own configuration
public struct Logging {
public typealias LogURLRequest = (URLRequest) -> Bool
public static var URLRequests: LogURLRequest = { _ in
#if DEBUG
return true
#else
return false
#endif
}
}
... is a set of classes that implement fully functional reactive data sources for UITableView
s and UICollectionView
s.
RxDataSources are bundled here.
Fully functional demonstration how to use them is included in the RxExample project.