Reputation: 1398
I have an observable sequence which represent my displayed sessions as follow :
private let displayedSessions =
PublishSubject<[ProgramDaySessionsModels.ViewModels.Session]>()
And here is my ViewModel:
struct ViewModels {
struct Session{
let id : Int?
let title : String?
let location : String?
var isAddedToCalendar : Bool?
var isAddedToFavorite : Bool?
let time : String?
}
}
The displayed sessions is being binded to a table view:
func setupCellBinding(){
displayedSessions
.asObservable()
.bind(to:
tableView
.rx
.items(cellIdentifier:
R.reuseIdentifier.programDaySessionCell.identifier,
cellType: ProgramDaySessionCell.self)
){
( row, element, cell ) in
cell.setup(withViewModel: element)
}
.disposed(by: disposeBag)
}
Now after i call the setup method in the cell, i want to subscribe to an image tap inside my cell, regardless what is the subscription logic will be ....
I want after that to update my displayed viewModel , for example i want to change the property isAddedToCalendar
to false/true when the subscription logic end...
My question is , How to update or change a value stored in the Subject
without calling onNext
operator
Upvotes: 2
Views: 3549
Reputation: 33967
ReactiveX is a functional paradigm. You don't "change the property," rather you emit a new property.
You could get close to doing what you want by having your initial array be an Observable of Observables Observable<[Observable<Session>]>
. Then you can push a new session through one element without pushing the entire array. Fair warning though, this can quickly become a management nightmare.
Better, I think would be to have an Observable<[Int]>
which contains session IDs and is used to populate the tableView and a separate Observable<[Int: Observable<Session>]>
for referring to the values in each cell.
Each cell would get an ID from the first observable, and use that ID to find and subscribe to it's Session in the second. Updating an observable in the dictionary would not cause the entire table view to reload.
It's still kind of hard to manage. Maybe make the dictionary Observable<[Int: Session]>
instead, make your Sessions Equatable, and use distintUntilChanged()
to avoid reloading a cell unless something has actually changed in that particular session.
Also, see my answer here: RxSwift : manage object updates in the app
Upvotes: 3
Reputation: 9382
You can't.
This is a very weird misconception people using Rx have, so It's important to highlight:
Subject
s, and Observable
s in general do NOT have a value, since an Observable is a stream of values and doesn't represent a single value.
Adding a value onto a Subject is done using onNext
(or .on(.next...)
) according to the ReactiveX standards.
The fact you think the value should be separated from the onNext
events makes it seem to me that you need to go back to basics and read some of the Rx/ReactiveX/RxSwift documentation to understand the basic concepts of Observable streams.
Good luck!
Upvotes: 2