mert
mert

Reputation: 1098

RxSwift how to refresh BehaviourSubject better way?

I have BehaviourSubject and I want to refresh items with last emitted value. I can do it example below,

 func refreshCurrent() {
    do {
        let items = try currentMatchList.value()
        if !(items.first?.items ?? []).isEmpty {
            refreshItems(sportId: try currentSport.value())
        }
    } catch {
        LOG.error(error.localizedDescription)
        return
    }
}

But I was wondering is there any built in RxSwift functionality I might use for same task?

I've found there was a Variable() type once upon a time but now it's gone and it's recommended to use BehaviourSubject it seems.

Thanks in advance.

Upvotes: 0

Views: 993

Answers (2)

mert
mert

Reputation: 1098

After searching all the issues in the official github repo I've found long discussion about same problem here and it's closed.

BUT good news is as freak4pc states we can use RxCocoa class BehaviourRelay and it has a direct value access method

example("BehaviorRelay") {
    let disposeBag = DisposeBag()
    let subject = BehaviorRelay<String>(value: "🚨")

    print(subject.value)

    subject.addObserver("1").disposed(by: disposeBag)
    subject.accept("🐶")
    subject.accept("🐱")

    print(subject.value)

}

Upvotes: 2

PeteSabacker
PeteSabacker

Reputation: 216

Don't know if I get you correctly, but it seems like you want to store your value within a BehaviourSubject.

let foo = BehaviourSubject<[Something]>(value: [])
print(foo.value) //Empty Array
foo.accept([Something(), Something()])
print(foo.value) //Array of two somethings

Upvotes: 0

Related Questions