RxSwift - change specific object in PublishSubject on event

Viewed 783
struct Contact : Codable, Hashable {
    var id : String
...
}

I use PublishSubject to feed the data to the UITableView

let contacts : PublishSubject<[Contact]> = PublishSubject()

And when the value is changed on the other view controller, I want to change the specific value in the array.

I want to change the Contact object with the specific id.

contacts.filter {$0.id == contactId}[0].someKey = someValue

How can I do this with RxSwift?

1 Answers

Understand that a PublishSubject doesn't contain any state so there is nothing in it that you can change. Instead, you emit a new array from the publish subject with a new contact that has the new value.

Somewhere in your code, you are calling onNext(_:) on the subject (or connecting it to an Observable that is doing that. We would need to see that code to help you solve your problem.

Related