Observe read only variable

602 views Asked by At

Can I observe read only variable by RxSwift ?

I'm trying observe variable from extendal library (iCarousel)

This is my code:

carouselView.rx.observe(Bool.self, "isScrolling")
    .map { $0 != nil }
    .subscribe(onNext: { (isScrolling) in
        print("isScrolling", isScrolling)
    }).addDisposableTo(disposeBag)

Console print this but only one time:

isScrolling, false
2

There are 2 answers

3
Daniel Poulsen On

I see it now. This is equal to

.map { $0 != nil } 

.map{return ($0 != nil)}

so you are not return weather or not it is nil but the boolean value of it being nil. what you want to use is filter

like

carouselView.rx.observe(Bool.self, "isScrolling")
    .filter { $0 != nil }
    .subscribe(onNext: { (isScrolling) in
        print("isScrolling", isScrolling)
    }).addDisposableTo(disposeBag)
1
Daniel Poulsen On

could it be because it is a User scrolling and not a programatic scrolling?

this is from iCarousel documentation.

@property (nonatomic, readonly, getter = isScrolling) BOOL scrolling;

Returns YES if the carousel is currently being scrolled programatically.

if that is the case you might want to use these in a combination

@property (nonatomic, readonly, getter = isDragging) BOOL dragging;

Returns YES if user has started scrolling the carousel and has not yet released it.

@property (nonatomic, readonly, getter = isDecelerating) BOOL decelerating;

Returns YES if the user isn't dragging the carousel any more, but it is still moving.