Reputation: 75
I have an BehaviorSubject array. i want to remove a photo from this array by id.
i tried;
photosViewModel.favoritePhotos
.observeOn(MainScheduler.instance)
.map { photoList in
photoList.filter { $0.id! != self.currentPhoto.id! } //remove currentPhoto
}.bind(to: photosViewModel.favoritePhotos)
.disposed(by: disposeBag)
Upvotes: 1
Views: 836
Reputation: 33967
You can't "remove an element" from a BehaviorSubject. The closest you can come is to insert a new array into the subject (which will cause it to emit that new array.) Something like this:
favoritePhotos.onNext(try favoritePhotos.value().filter { $0.id != currentPhoto.id })
Upvotes: 2