RxSwift General order of subscription
How can I ensure that the subscriber does not Observable
receive the event onNext
after another subscriber?
My example is as follows:
let firstNameObservable = firstName.asObservable().shareReplay(1)
let lastNameObservable = lastName.asObservable().shareReplay(1)
let bioObservable = bio.asObservable().shareReplay(1)
let websiteObservable = website.asObservable().shareReplay(1)
firstNameObservable.subscribe(onNext: { [unowned self] firstName in self.accountDetails.firstName = firstName }).disposed(by: disposeBag)
lastNameObservable.subscribe(onNext: { [unowned self] lastName in self.accountDetails.lastName = lastName }).disposed(by: disposeBag)
bioObservable.subscribe(onNext: { [unowned self] bio in self.accountDetails.bio = bio }).disposed(by: disposeBag)
websiteObservable.subscribe(onNext: { [unowned self] website in self.accountDetails.website = website }).disposed(by: disposeBag)
Observable.combineLatest(firstNameObservable, lastNameObservable, bioObservable, websiteObservable)
{ [unowned self] _, _, _, _ in return self.accountDetails.validForSignUp }
.bind(to: isValid)
.disposed(by: disposeBag)
I would like the last combineLatest
binding to the trigger after the previous one has already executed 4 subscriptions
. This is because only after the properties have been set accountDetails
that will validForSignUp
.
I know the solution to this problem would be to do validForSignUp
a Variable
and watch for it, but suppose that is not possible.
source to share
I would do something like this:
let accountDetails = AccountDetails(firstName: firstName.orEmpty.asObserable(), lastName: lastName.orEmpty.asObservable(), bio: bio.orEmpty.asObservable(), website: website.orEmpty.asObservable())
accountDetails.validForSignup
.bind(to: isValid)
.disposed(by: bag)
struct AccountDetails {
let validForSignup: Observable<Bool>
init(firstName: Observable<String>, lastName: Observable<String>, bio: Observable<String>, website: Observable<String>) {
let firstNameValid = firstName.map { $0.isValidFirstName }
let lastNameValid = lastName.map { $0.isValidLastName }
let bioValid = bio.map { $0.isValidBio }
let websiteValid = website.map { $0.isValidWebsite }
validForSignup = Observable.combineLatest([firstNameValid, lastNameValid, bioValid, websiteValid]) { $0.allTrue() }
}
}
extension String {
var isValidFirstName: Bool {
return !isEmpty // put approprate code here
}
var isValidLastName: Bool {
return !isEmpty // put approprate code here
}
var isValidBio: Bool {
return !isEmpty // put approprate code here
}
var isValidWebsite: Bool {
return !isEmpty // put approprate code here
}
}
extension Sequence where Iterator.Element == Bool {
func allTrue() -> Bool {
return !contains(false)
}
}
source to share
You can handle data validation with a single subscription. Here's how it can be done when I bind the combined events to an observable that does validation on the combined results.
let fields = [firstNameObservable,
lastNameObservable,
bioObservable,
websiteObservable]
let validFields = Observable.combineLatest(fields).bind(to: validateFields)
An observable that checks the merged results might look something like this:
func validateFields<T>(allFields: Observable<[T]>) -> Observable<Bool>
{
return allFields.map { fieldData in
var result: Bool = false
/* Is the data valid? */
return result;
}
}
The Observable validFields
will be updated with the validation result every time new data comes in from any of the Observables fields. With help combineLatest
, you have the added benefit of maintaining the order of your observables. This solution is achieved without resorting to storing state, making it completely reactive.
source to share