Search code examples
swiftrx-swiftuiactivityindicatorviewindicator

How to use RxSwift with NVActivityIndicatorView?


I'm trying to use NVActivityIndicatorView with RxSwift and MVVM. I'm extending NVActivityIndicatorView like below :

extension Reactive where Base: NVActivityIndicatorView {

/// Bindable sink for `startAnimating()`, `stopAnimating()` methods.
public var isAnimating: Binder<Bool> {
    return Binder(self.base) { activityIndicator, active in
        if active {
            activityIndicator.startAnimating()
        } else {
            activityIndicator.stopAnimating()
        }
    }
}}

And binding data like this:

splashVM.loading.bind(to: NVActivityIndicatorView.rx.isAnimating).disposed(by: disposeBag)

but i get tis error :

Instance member 'isAnimating' cannot be used on type 'Reactive<NVActivityIndicatorView>'

if I set isAnimation static ,that error goes away but another error pops up :

Instance member 'base' cannot be used on type 'Reactive<Base>'

what's the problem exactly? what should i change?


Solution

  • You are binding it wrong. You need to bind to a specific activity indicator view not the class:

    let activityIndicatorView: NVActivityIndicatorView
    
    splashVM.loading
        .bind(to: activityIndicatorView.rx.isAnimating)
        .disposed(by: disposeBag)