I need some internal state in a viewModel but also trying to follow the "no subscription / bind / drive / ..." ideal approach and only compose between Observables.
How can I specify what a Variable observes?
Example:
private var userProfilesToFetch = Variable<[String]>([])
private var users: Variable<[User]> {
return //something that observes fetchUserProfiles() and when it emits, appends to its .value
}
private func fetchUserProfiles() -> Observable<User?> {
let reference = databaseRef.child("users")
return userProfilesToFetch.asObservable()
.filter({ $0 != [] })
.map({ $0.last! })
.flatMap({ (userId) -> Observable<User?> in
return self.service.observeAllChildren(of: reference.child(userId), by: .value)
.map({ (snapshot) -> User? in
guard let values = snapshot.value as? [String: AnyObject] else { return nil }
var user = User(dictionary: values)
user.id = snapshot.key
return user
})
})
}
Variablein the subscription chain. For example if in my VC I subscribe to (A) in my viewModel, then because this (A) is bound to another observable in its definition, like thefetchUserProfiles()function above, it will chain subscribe touserProfilesToFetchand so on. What I can't figure out is how to chain the Variable itself by specifying what it should observe. - Herakleis