在RxSwift中订阅Observable或Driver需要太长时间

时间:2021-08-11 21:21:57

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

我注意到RxSwift减慢了应用程序的速度,我不知道这是我的错还是框架。

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

我在UICollectionViewCell的自定义类中绑定了,这是为配置单元格而触发的。例如这部分代码:

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.

where observableIsHighlighted,observableIsMarked只是BehaviorRelay ,需要大约16-20毫秒,这当然是UICollectionView无法接受的。

Another part like this:

另一部分是这样的:

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

takes around 12 milliseconds.

需要大约12毫秒。

Question

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

我的方法是否有问题,或者RxSwift中的绑定对于像UICollectionView这样的视图是不准确的?

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

当然,我假设RxSwift中的绑定完全适用于像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.

我使用CACurrentMediaTime()乘以1000.我知道这是最好的方法,但是非常简单的方法总是花费0毫秒,所以它建议10-20毫秒的方法,应该改进。

1 个解决方案

#1


2  

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.

我无法相信,但我在我的Podfile中从4.0更新了RxSwift和RxCocoa到4.1.0,现在它完美无缺。

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

问题的方法现在大约需要0-1毫秒。

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

希望这个问题可以帮助别人,也许任何人都会知道为什么我会遇到这种行为。

#1


2  

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.

我无法相信,但我在我的Podfile中从4.0更新了RxSwift和RxCocoa到4.1.0,现在它完美无缺。

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

问题的方法现在大约需要0-1毫秒。

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

希望这个问题可以帮助别人,也许任何人都会知道为什么我会遇到这种行为。