TheTeacher33 TheTeacher33 -4 years ago 57
Swift Question

Label is not updated, why?

func getLastPriceForFromBtceForPair()->(ethLast:String,btcLast:String){

var btcUSDLast = ""
var ethUSDLast = ""

let APIURL = "https://btc-e.com/api/3/ticker/btc_usd)"
let APIURL2 = "https://btc-e.com/api/3/ticker/eth_usd)"

Alamofire.request(APIURL).responseData { (resData) in

let prices = JSON(data: resData.result.value!)


if let pairGeneral = prices["btc_usd"].dictionaryObject {

if let pairLast = pairGeneral["last"] as? Double{

btcUSDLast = String(pairLast)

//If I print here I can see the data.

}

}

}


Alamofire.request(APIURL2).responseData { (resData2) in

let prices2 = JSON(data: resData2.result.value!)


if let pairGeneralETH = prices2["eth_usd"].dictionaryObject {

if let pairLastETH = pairGeneralETH["last"] as? Double{

ethUSDLast = String(pairLastETH)

//If I print here I can see the data.

}

}

}

return (btcUSDLast,ethUSDLast)
}


This is my code... I am trying to get data from btc-e.I have a button and when it is pressed this function gets called and should update the label.

self.ethereumPriceUSDLabel.text = getLastPriceForFromBtceForPair().ethLast


If I put print statements to the places I marked in code the data gets printed.But I cannot get my labels updated in the app,they are empty. Any ideas why? Thanks in advance

Answer Source

Because Alamofire.request(APIURL2).responseData is an asynchronous method. The closures in { } will be executed at some point of time in the future when request will be done. I think you need to plan both requests to be done with closures and put them in a dispatch group. And when both requests are done it will execute the closure "completion" on the main thread so you could update your labels in that closure. A dispatch group gives you the ability to execute both requests at the same time and wait for both of them to be done. In real life i'd rather do two functions: one for each request. But it's all up to your app architecture.

func getLastPriceForFromBtceForPair(completion: @escaping (_ ethLast:String, _ btcLast:String)->Void){

    var btcUSDLast = ""
    var ethUSDLast = ""

    let APIURL = "https://btc-e.com/api/3/ticker/btc_usd)"
    let APIURL2 = "https://btc-e.com/api/3/ticker/eth_usd)"

    let group = DispatchGroup()

    group.notify(queue: .main) { 
        completion(btcUSDLast, ethUSDLast)
    }

    group.enter()
    Alamofire.request(APIURL).responseData { (resData) in

        let prices = JSON(data: resData.result.value!)

        if let pairGeneral = prices["btc_usd"].dictionaryObject {
            if let pairLast = pairGeneral["last"] as? Double{
                btcUSDLast = String(pairLast)
            }
        }
        group.leave()
    }

    group.enter()
    Alamofire.request(APIURL2).responseData { (resData2) in

        let prices2 = JSON(data: resData2.result.value!)

        if let pairGeneralETH = prices2["eth_usd"].dictionaryObject {
            if let pairLastETH = pairGeneralETH["last"] as? Double{
                ethUSDLast = String(pairLastETH)
            }
        }
        group.leave()
    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download