Context: My team is starting a new medium size Swift project (of around 20 MM) and I am thinking to develop it in RxSwift
. One of my managers suspects that once he had a bad experience about debugging on Reactive Programming so he suggests to avoid RxSwift and go with classical Swift. I am trying to find out the disadvantages of RxSwift and I found none of them, mentioned about the debuggability issues.
Question: What are the factual technical limitations of RxSwift for debuggability?
I have been using RxSwift since 2015 when it first came out. I have put it in lots of small and medium projects over the years. I wouldn't call it difficult to debug but it is different to debug. As @AjinkaSharma mentioned in the comments, following a stack trace is a very frustrating experience so don't even bother. Better is to put .debug()
operators at strategic places. Another useful tool is [TimeLane][1] which will allow you to track your observables in the profiler.
More importantly, if you are having even a little bit of a hard time getting an observable chain to work the way you want, write a unit test. It is extremely easy to wrap a bit of logic into an operator and then write a test for it using RxTest and I don't know why more people don't do it other than they are used to unit tests being a pain when using MVC.
In my experience, the hardest part to debug are resource leaks. Even detecting them can be difficult unless you include the TRACE_RESOURCES
flag in your debug build. Once you do that, you can put the following in your app delegate:
#if DEBUG
_ = Observable<Int>.interval(.seconds(1), scheduler: MainScheduler.instance)
.map { _ in RxSwift.Resources.total }
.distinctUntilChanged()
.subscribe(onNext: { print("♦️ Resource count \($0)") })
#endif
That way you can track the resource count when you go back and fourth between screens to make sure everything is released properly.
Lastly, the architecture you end up with is quite functional and very different than what most Swift developers are used to. When done right, you end up with more closures and free functions and fewer protocols and extensions, so that will take getting used to as well.
Join us on the RxSwift slack where there are a bunch of knowledgable and active participants who will be happy to answer quick questions or share a bit of code any time of the day.