itsji10dra
itsji10dra

Reputation: 4675

Observable<String?> runs only first time

My Structure,

internal struct KeychainManager {    
    static private(set) var accessToken: String? = nil
}

My UI bind code,

override func viewDidLoad() {
    super.viewDidLoad()

    let observableToken = Observable.just(KeychainManager.accessToken)

    observableToken
        .debug()
        .map { $0 == nil }
        .bind(to: authenticateButton.rx.isEnabled)
        .disposed(by: disposeBag)
}

When my app opens, authenticateButton can be seen enabled, but when I make API call then after saving my token successfully in KeychainManager.accessToken, authenticateButton still remains enabled. Why?

What's going wrong here?

Any suggestions will be helpful.

Upvotes: 0

Views: 160

Answers (1)

pacification
pacification

Reputation: 6018

. just takes an argument and sends it as .next and then send .completed right after the .next. It means that .just called only once. But you need really observe every change of accessToken value. Two ways to achieve that:

  1. by using notifications: post notification when it changed and use rx notification wrapper to handle changes;
  2. by using any of *Subject. In this case, after accessToken set you should manually call subject.onNext(accessToken) (send event that accessToken changed) and handle this subject in your controller.

Upvotes: 1

Related Questions