网络调用后绑定到 UIRefreshControl
Binding to a UIRefreshControl after network call
我是 RxSwift 的新手,我想知道我如何能够 "reactively" 将 UIRefreshControl 与 UITableView 一起使用,而不是创建目标并手动调用 beginRefreshing()
和endRefreshing()
.
例如,假设我正在从 API:
加载一些字符串
class TableViewController: UITableViewController {
var data : [String] = []
let db = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl = UIRefreshControl()
//I don't want to use
//refreshControl?.addTarget(self, action: #selector(getData), forControlEvents: .ValueChanged)
//Do something to refreshControl.rx_refreshing?
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let str = data[indexPath.row]
cell.textLabel?.text = str
return cell
}
//MARK: - Requests
private func getData() {
let myData = MyAPI.getData() //Returns Observable<[String]>
myData
.subscribe({ [weak self] (event) in
switch event {
case .Next(let strings):
self?.data = strings
self?.tableView.reloadData()
break
case .Error(let err):
print(err)
break
case .Completed:
break
}
// self?.refreshControl?.endRefreshing()
})
.addDisposableTo(db)
}
}
MyAPI
发送对某些字符串值的请求,我如何绑定 refreshControl
以调用 getData()
并在网络完成(或错误)时停止刷新要求?我需要绑定到 refreshControl.rx_refreshing 吗?
RxSwift 的示例应用程序提供了一个有趣的 class 来处理这种逻辑:ActivityIndicator
.
输入 ActivityIndicator
后,将 rx_refreshing
绑定到请求的代码就变得非常简单。
let activityIndicator = ActivityIndicator()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl = UIRefreshControl()
// When refresh control emits .ValueChanged, start fetching data
refreshControl.rx_controlEvent(.ValueChanged)
.flatMapLatest { [unowned self] _ in
return self.getData()
.trackActivity(activityIndicator)
}
.subscribeNext { [unowned self] strings in
self.data = strings
self.tableView.reloadData()
}
.addDisposableTo(db)
// Bind activity indicator true/false to rx_refreshing
activityIndicator.asObservable()
.bindTo(refreshControl.rx_refreshing)
.addDisposableTo(db)
}
// getData only needs to return an observable, subscription is handled in viewDidLoad
private func getData() -> Observable<[String]> {
return myData = MyAPI.getData() //Returns Observable<[String]>
}
我是 RxSwift 的新手,我想知道我如何能够 "reactively" 将 UIRefreshControl 与 UITableView 一起使用,而不是创建目标并手动调用 beginRefreshing()
和endRefreshing()
.
例如,假设我正在从 API:
加载一些字符串class TableViewController: UITableViewController {
var data : [String] = []
let db = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl = UIRefreshControl()
//I don't want to use
//refreshControl?.addTarget(self, action: #selector(getData), forControlEvents: .ValueChanged)
//Do something to refreshControl.rx_refreshing?
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath)
let str = data[indexPath.row]
cell.textLabel?.text = str
return cell
}
//MARK: - Requests
private func getData() {
let myData = MyAPI.getData() //Returns Observable<[String]>
myData
.subscribe({ [weak self] (event) in
switch event {
case .Next(let strings):
self?.data = strings
self?.tableView.reloadData()
break
case .Error(let err):
print(err)
break
case .Completed:
break
}
// self?.refreshControl?.endRefreshing()
})
.addDisposableTo(db)
}
}
MyAPI
发送对某些字符串值的请求,我如何绑定 refreshControl
以调用 getData()
并在网络完成(或错误)时停止刷新要求?我需要绑定到 refreshControl.rx_refreshing 吗?
RxSwift 的示例应用程序提供了一个有趣的 class 来处理这种逻辑:ActivityIndicator
.
输入 ActivityIndicator
后,将 rx_refreshing
绑定到请求的代码就变得非常简单。
let activityIndicator = ActivityIndicator()
override func viewDidLoad() {
super.viewDidLoad()
refreshControl = UIRefreshControl()
// When refresh control emits .ValueChanged, start fetching data
refreshControl.rx_controlEvent(.ValueChanged)
.flatMapLatest { [unowned self] _ in
return self.getData()
.trackActivity(activityIndicator)
}
.subscribeNext { [unowned self] strings in
self.data = strings
self.tableView.reloadData()
}
.addDisposableTo(db)
// Bind activity indicator true/false to rx_refreshing
activityIndicator.asObservable()
.bindTo(refreshControl.rx_refreshing)
.addDisposableTo(db)
}
// getData only needs to return an observable, subscription is handled in viewDidLoad
private func getData() -> Observable<[String]> {
return myData = MyAPI.getData() //Returns Observable<[String]>
}