I have the Tableview with UIAlertAction. When I press a cell, a pop-up appears and then I can decide which operation to perform. During the Ok operation (button click) I want to observe the data of the cell and receive it in the ViewModel. But there are problems. When I press for the first time there is no value and if I just click a cell the data is still sending to the ViewModel. If I click more UIAlertAction popu-ups the sending data is increasing. For example: First click - nothing, second click - value 1, third click value 1, 1, 1 and so on. How to start observing cells from the first click of the UIAlertAction pop-up and how to get only on example of the data?
ViewController:
func bindTableView() {
viewModel.stationItems.bind(to: addTableView.rx.items(cellIdentifier: "addCell", cellType: AddTableViewCell.self)) { (row, item, cell)
in
cell.cellAdd = item
}.disposed(by: disposeBag)
addTableView.rx.modelSelected(StationItem.self)
.subscribe(onNext: { item in
let alert = UIAlertController(title: "Add Station", message: "Do you want to add a station to your favorites?", preferredStyle: .alert)
let ok = UIAlertAction(title: "Ok", style: .default, handler: { action in
self.addTableView.rx.modelSelected(StationItem.self)
.bind(to: self.viewModel.stationItem)
.disposed(by: self.disposeBag)
self.viewModel.addStationItem()
})
alert.addAction(ok)
let cancel = UIAlertAction(title: "Cancel", style: .default, handler: { action in
})
alert.addAction(cancel)
DispatchQueue.main.async(execute: {
self.present(alert, animated: true)
})
}).disposed(by: disposeBag)
viewModel.fetchStations()
}
ViewModel:
let stationItem = PublishSubject<StationItem>()
func addStationItem() {
stationItem.subscribe(onNext: {(data) in
print("Data: \(data)")
})
.disposed(by: disposeBag)
print("ADD:")
}