1
我試圖調用兩個單獨的api端點來返回一些json,兩個調用將對象添加到列表中...我如何等待這兩個funcs該列表返回到視圖控制器Swift,在返回列表之前等待兩個完成塊完成
import Alamofire
import SwiftyJSON
class SearchService {
var searchItemList = [SearchItem]()
func performFetch(keyword: String, count: Int, completion:(items: [SearchItem]?) ->()){
>>>// NEED TO WAIT FOR THE fetchItemsA AND fetchItemsB TO FINISH THEM RETURN ‘searchItemList’
}
func fetchItemsA(keyword: String, count: Int, completion:(error:NSError?) ->()) {
guard let url = self.searchUrl else {
return
}
let manager = Manager.sharedInstance
manager.request(.GET, url, parameters: ["keywords": keyword, "count": count], encoding: ParameterEncoding.URL)
.validate()
.responseJSON {response in
…
for item in items {
self.searchItemList.append(SearchItem(json: item)!)
}
…
completion(error: resultError)
}
func fetchItemsB(keyword: String, count: Int, completion:(error:NSError?) ->()) {
guard let url = self.searchUrl else {
return
}
let manager = Manager.sharedInstance
manager.request(.GET, url, parameters: ["keywords": keyword, "count": count], encoding: ParameterEncoding.URL)
.validate()
.responseJSON {response in
…
for item in items {
self.searchItemList.append(SearchItem(json: item)!)
}
…
completion(error: resultError)
}
}
任何幫助表示讚賞
難道你不能把搜索謂詞結合起來使用**一個** API調用嗎? – vadian
乾杯@vadin,你讓我想... http://stackoverflow.com/q/28791349/4154338 – rogger2016