I have existing and working code, which looks like this (unnecessary stuff removed):
queryTextField.rx.text.orEmpty
.throttle(.milliseconds(300), scheduler: MainScheduler.instance)
.distinctUntilChanged()
.flatMapLatest { query in
return try AddFriendViewController.getSearchResults(query)
.retry(3)
.startWith([])
.catchErrorJustReturn([])
}
.bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: AddFriendTableViewCell.self)) { (row, item, cell) in
cell.nameLabel.text = "\(item.firstName) \(item.lastName)"
}
.disposed(by: disposeBag)
When you type a name in a text field it automatically searches (through an API) for friends matching this query.
But I also want to add another feature - import from contacts, invoked by additional button. I have a list of local contacts and using them I ask API for search results. I want to display these results in the same place. But I don't know how to send these results into tableView. I thought of creating a varabiable:
var items: Observable<[FriendSearchResult]> = Observable.just([])
But I don't know how to send/receive data. Any clues?
edit:
Thanks to @andromedainative now code looks like this:
var items: BehaviorRelay<[FriendSearchResult]> = BehaviorRelay(value: [])
items.asObservable()
.bind(to: tableView.rx.items(cellIdentifier: "cell", cellType: AddFriendTableViewCell.self)) { (row, item, cell) in
cell.nameLabel.text = "\(item.firstName) \(item.lastName)"
}
.disposed(by: disposeBag)
queryTextField.rx.text.orEmpty
.throttle(.milliseconds(300), scheduler: MainScheduler.instance)
.distinctUntilChanged()
.flatMapLatest { query in
return try AddFriendViewController.getSearchResults(query)
.retry(3)
.startWith([])
.catchErrorJustReturn([])
}
.bind(to: items) //this was a missing piece
.disposed(by: disposeBag)
And passing results from getContacts is simple:
items.accept(data)
But, I have a special case where I have to refresh data. How can I invoke queryTextField to call API with the same query?
I've only found a hacky way:
let query = queryTextField.text ?? ""
queryTextField.text = ""
queryTextField.sendActions(for: .valueChanged)
queryTextField.text = query
queryTextField.sendActions(for: .valueChanged)
I have to change to some other value and change it back. Is there any more clean solution?