Tuesday, June 16, 2026
HomeiOS Developmentios - RxSwift - Figuring out whether or not an Observable has...

ios – RxSwift – Figuring out whether or not an Observable has been disposed

[ad_1]

I am attempting to get Writer which vends Observables to its shoppers Client, to find out when one in all its customers has disposed of its Observable.

Annoyingly. my code was working wonderful, till I eliminated an RxSwift .debug from throughout the Client code.

Is there some various means I would get this working?

non-public class Subscriber {
    var ids: [Int]
    // This property exists so I can watch whether or not the observable has 
    // gone nil (which I although would occur when its been disposed, but it surely 
    // appears to occur instantly)
    weak var observable: Observable<[Updates]>?
}
class Writer {
    non-public let relay: BehaviorRelay<[Int: Updates]> 
    non-public var subscribers: [Subscriber] = []

    func updatesStream(for ids: [Int]) -> Observable<[Updates]> {
        let observable = relay
           .map { map in
               return map
                   .filter { ids.comprises($0.key) }
                   .map { $0.worth }
           }
           .filter { !$0.isEmpty }
           .asObservable()

        let subscriber = Subscriber(ids: ids, observable: observable)
        subscribers.append(subscriber)
        return observable
    }

    non-public func repeatTimer() {
        let updates: [Updates] = ....

        // I want to have the ability to decide at this level whether or not the subscriber's 
        // observable has been disposed of, so I can take away it from the checklist of
        // subscribers. Nonetheless `subscriber.observable` is all the time nil right here.
        // PS: I'm joyful for this to occur earlier than the `repeatTimer` func fires
        subscribers.take away(the place: { subscriber in
            return subscriber.observable == nil
        })

        relay.settle for(updates)
    }
}

class Shopper {

    non-public var disposeBag: DisposeBag?
    non-public let writer = Writer()

    func startWatching() {
        let disposeBag = DisposeBag()
        self.disposeBag = disposeBag

        writer
            // with the `.debug` under issues work OK, with out it the 
            ///`Writer.Subscriber.observable` instantly turns into nil
            //.debug("Client") 
            .updatesStream(for: [1, 2, 3])
            .subscribe(onNext: { values in
                print(values)
            })
           .disposed(by: disposeBag)
    }

    func stopWatching() {
        disposeBag = nil
    }
}

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments