I have an app that uses the delegate method to communicate controllers with ViewModels. But I would like to start using reactive programming with RxSwift.
The problem I have is that I don't know what subject to use.
The first thing I need is an observable that can notify me when an array of data has changed, but that allows me to add values, modify values, and remove values. And that it can be binded to a tableView.
Is it okay to use BehaviorRelay?
public var array = BehaviorRelay<[Struct]>(value: [])
Example with delegate:
var posts: [PostStruct] {
didSet {
//Call delegate: didUpdatePosts(posts) -> Update TableView
}
}
//Also to allow:
posts.append(newPost)
posts.remove(at: index)
posts[index].name = "Hi"
and in the second occasion where I need to use RxSwift is to notify me of changes that have occurred in a property so that I can update the UI.
Example with delegate:
var post: PostStruct {
didSet {
//Update UI
//Call delegate: didUpdatePost(post) -> Update UI
}
}
The posts are obtained from the database, so it is important to update the table view every time elements are added or deleted.
So... basically what I'm looking for is an observable that notifies me when its value is changed, removed, or added, just like @Published on SwiftUI.
Thanks