从异步完成处理程序返回的RXSwift flatmap

时间:2017-03-10 12:34:10

标签: swift rx-swift rx-cocoa

我正在尝试创建一个searchBar,它通过带有请求的MKLocalSearch搜索地址,并使用RXSwift并绑定到RXCocoa

到目前为止,我已做了以下工作 1.过滤和去抖以避免过多的请求:

let searchRes = searchBar.rx.text
    .orEmpty
    .filter { query in
        return query.characters.count > 4
    }
    .debounce(1, scheduler: MainScheduler.instance)
  1. 生成的String查询是map:ed以创建MKLocationSearch,然后是flatMapp:ed以返回MKMapItems数组,以便能够将数组绑定到tableView的项目:

    searchRes.map{query -> MKLocalSearch in
        let request = MKLocalSearchRequest()
        request.naturalLanguageQuery = query
        request.region = self.mapView.region
        return MKLocalSearch(request: request)
    }.flatMapLatest{search -> Observable<[MKMapItem]> in
        search.start(completionHandler:{(response, error) in
            let items: Variable<[MKMapItem]> = Variable([])
            if let resp = response {
                //need to return the result form this
                // i.e. items.value = response.mapItems
            }
            //can not return from here since the request is async
        }
    }.bindTo //continue to bind to tableview
    
  2. 如何使用RXSwift完成,我找不到任何相关的例子

1 个答案:

答案 0 :(得分:5)

您必须使用ansync请求创建一个Observable。您可以定义以下方法来执行此操作:

_

然后你可以使用func mapItems(for searchRequest: MKLocalSearch) -> Observable<[MKMapItem]> { return Observable.create { observer in searchRequest.start(completionHandler: { (response, error) in if let error = error { observer.onError(error) } else { let items = response?.mapItems ?? [] observer.onNext(items) observer.onCompleted() } }) return Disposables.create { searchRequest.cancel() } } } 方法:

flatMapLatest