func getDataFromDatamanager() {
DataManager.getGoldPriceFromQuandlWithSuccess { (QuandlGoldPrice) -> Void in
let json = JSON(data: QuandlGoldPrice)
if let datasetMineral = json["dataset"]["dataset_code"].string {
print("NSURLSession: \(datasetMineral)")
}
DataManager.getSilverPriceFromQuandlWithSuccess { (QuandlSilverPrice) -> Void in
let json = JSON(data: QuandlSilverPrice)
if let datasetMineral = json["dataset"]["dataset_code"].string {
print("NSURLSession: \(datasetMineral)")
}
}
此功能中还有大约15个其他呼叫,它们都需要不同的下载时间。 我正在调用函数来操作主线程并启动一个活动指标:
dispatch_async(dispatch_get_main_queue(), {
self.activityIndicator.startAnimating()
self.getDataFromDatamanager()
})
我的问题:如果下载了所有功能,我怎么能停止活动指示器?
答案 0 :(得分:1)
介绍completedItems:变量。某些功能下载完成后可以增加。每次完成后都会调用停止活动功能,如下所示: -
var completedItems:Int = 0
func getDataFromDatamanager() {
DataManager.getGoldPriceFromQuandlWithSuccess { (QuandlGoldPrice) -> Void in
let json = JSON(data: QuandlGoldPrice)
if let datasetMineral = json["dataset"]["dataset_code"].string {
completedItems = completedItems + 1 // completedItems ++
stopActivity()
print("NSURLSession: \(datasetMineral)")
}
DataManager.getSilverPriceFromQuandlWithSuccess { (QuandlSilverPrice) -> Void in
let json = JSON(data: QuandlSilverPrice)
if let datasetMineral = json["dataset"]["dataset_code"].string {
completedItems = completedItems + 1 // completedItems ++
stopActivity()
print("NSURLSession: \(datasetMineral)")
}
}
func stopActivity() {
if completedItems == 15 { // give # of functions
dispatch_sync(dispatch_get_main_queue(), {
self.activityIndicator.stopAnimating()
})
}
}