Search code examples
iosswiftrx-swiftreactivexrx-cocoa

Subscribing to Observable or Driver takes too long in RxSwift


I noticed that RxSwift slows down the app, I don't know if this is my fault or the framework.

I have binding inside custom class of UICollectionViewCell, which is fired for configuring cell. For example this part of code:

viewModel.observableIsHighlighted.asDriver().drive(onNext: { isHighlighted in
    // do nothing
}).disposed(by: disposeBag)
viewModel.observableIsMarked.asDriver().drive(onNext: { isMarked in
    // do nothing
}).disposed(by: disposeBag)

where observableIsHighlighted, observableIsMarked are just BehaviorRelay<Bool>, takes around 16-20 milliseconds which is unacceptable for UICollectionView of course.

Another part like this:

imageViewTapGestureRecognizer.rx.event.bind(onNext: { _ in
    // do nothing
}).disposed(by: disposeBag)

takes around 12 milliseconds.

Question

Is it something wrong in my approach or binding in RxSwift is not accurate for views like UICollectionView?

Of course, I assume that binding in RxSwift is exactly for things like UICollectionView.

Edit

How am I measuring time?

I use CACurrentMediaTime() multiplied by 1000. I know it's the best way, but methods that are quite simple always take 0 ms, so it suggest that 10-20 ms methods, are should be improved.


Solution

  • I can't believe, but I updated RxSwift and RxCocoa to 4.1.0 from 4.0 inside my Podfile and now it works perfectly.

    Methods from the question take around 0-1 ms now.

    Hope this question will help others or maybe anyone would know why I experienced this behavior.