I have an app that have 3 views and a service logic layer. The services is sending messagings all the time to all this views.What I do to control all this data was use RxBehaviorRelay.
To this point all works great, I match the variables with the services layer and I am receiving data in the ViewController 1, 2 and 3.
The problem comes when I am on the view2 and then go to view3 and return to view2. On this return I lost all the changes and the BehaviorRelay object is like the beginning.
My code in the view1 is like this one
class View1: UIViewController {
var service: ServiceModel? = ServiceModel()
var observableView1 = BehaviorRelay(value: [Object]())
var observableView2 = BehaviorRelay(value: [Object]())
override func viewDidLoad() {
service!.observableView1 = self.observableView1
service!.observableView2 = self.observableView2
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let destination = segue.destination as! View2
destination.observableView2 = self.observableView2
}
}
Code of view2
class View2: UIViewController {
let disposeBag = DisposeBag()
var observableView2 = BehaviorRelay(value: [Object]())
override func viewDidLoad() {
fillTable()
}
func fillTable() {
observableView2.asObservable().bind(to: tvData.rx.items) { (tableview, row, data) in
//code to fill the table
}.disposed(by: disposeBag)
}
}
On the service layer I only receive the changes and put in the BehaviorRelay object.
observableView2.accept(changes)
observableView1.accept(changes)
As I said I obtain all the changes in the view2 but when I move to view3 and return to view2, the changes gone.
Sorry for my english. Thanks