Dive in Disposable Mechanisms in RxSwift.

Nikita Ermolenko
@RosberryApps
Published in
3 min readSep 13, 2017

Hey everyone! As a Reactive Lover, I’m going to continue a series of articles dedicated to RxSwift. In my first reactive post, I reasoned about some improvements of flatMap function (you can check it here) and today I want to extend my knowledge of RxSwift by exploring all Rx Disposables.

This article will explore all Disposables that are available in Rx with short explanations and usage examples.

So, let’s start! 🚀

1. Disposable.Empty

This can be useful whenever you need to fulfil an interface requirement and don’t have any resource management that needs to take place.

Under the hood it uses the private class — NopDisposable (Nop = No Operation)

2. Disposable.Create (action)

The second static method provides a disposable with action that is called when the Dispose method is invoked. It’s handy to use this method with server requests — you can control the lifetime of request with dispose block.

In this cases we can see another private class — AnonymousDisposable. If somebody says something about AnonymousDisposable, don’t be afraid — it’s just a Disposable with some dispose action :)

⚠️ I want to note that in the previous one, create.empty function returns the Disposable rather than Cancelable object like in this AnonymousDisposable. The differences between them are just in the ability to check the disposed state — isDisposed.

3. BooleanDisposable

This class simply has the Dispose method and a read-only property isDisposed that is false when the class is created, and is set to true when the Dispose method is invoked.

4. CompositeDisposable

Represents a group of disposable resources that are disposed together. Calling Dispose on the CompositeDisposable will call dispose on each of these resources in the order they were provided.

There’s one more way of creating a group of disposables using the private BinaryDisposable class, but not directly, via the static function of Disposables:

Additionally, the CompositeDisposable class allows you to add and remove resources. After the CompositeDisposable has been disposed of, any further resources that are added, will be disposed immediately.

Any item that is removed from CompositeDisposable is also disposed of, regardless of whether the main disposable itself has been disposed of.

5. RefCountDisposable

Represents a disposable resource that only disposes its underlying disposable resource when all dependent disposable objects have been disposed.

6. ScheduledDisposable

Represents a disposable resource whose disposal invocation will be scheduled on the specified scheduler.

the thread you see is not a main here.

7. SerialDisposable

Represents a disposable resource whose underlying disposable resource can be replaced by another disposable resource, causing automatic disposal of the previous underlying disposable resource.

8. SingleAssignmentDisposable 🤔

Represents a disposable resource which only allows a single assignment of its underlying disposable resource.

If an underlying disposable resource has already been set, future attempts to set the underlying disposable resource will throw an exception.

Conclusion

Finally I want to say that there’re so many different disposables, but it’s hard to find a really good usage for them IMO 😭. Please, leave a comment if you use some of the following disposable approaches and it helps you a lot.

If you want to find out more about these disposables, check my playground.

Hope you enjoyed this article! Feel free to comment if you have any question, feedback or best practices!

If you liked this article, go ahead and press the 👏👏👏👏 below.

I during ITC build processing :)

Shaggy iOS Engineer at Rosberry. Reactive, Open-Source lover and Retain-cycle detector :)

Also check my telegram channel about programming, twitter and my open-source projects:

--

--

Nikita Ermolenko
@RosberryApps

Shaggy iOS Software Developer. Open-source lover. Please, just awesome programmer.