Updating UI after retrieving device settings

时间:2018-05-14 17:30:17

标签: iphone swift asynchronous

I want to do something simple in Swift. I have to retrieve some setting from a device and then initialize some UI controls with those settings. It may take a few seconds to complete the retrieval so I don't want the code to continue until after the retrieval (async).

I have read countless posts on many websites including this one and read many tutorials. None seem to work for me.

Also, in the interest of encapsulation, I want to keep the details within the device object.

When I run the app I see the print from the initializing method before I see the print from the method.

// Initializing method

brightnessLevel = 100
device.WhatIsTheBrightnessLevel(level: &brightnessLevel)
print("The brightness level is \(brightnessLevel)")


// method with the data retrieval code

func WhatIsTheBrightnessLevel(level brightness: inout Int) -> CResults
{
var brightness: Int
var characteristic: HMCharacteristic
var name: String
var results: CResults
var timeout: DispatchTime
var timeoutResult: DispatchTimeoutResult



// Refresh the value by querying the lightbulb
name = m_lightBulbName
characteristic = m_brightnessCharacteristic!
brightness = 100
timeout = DispatchTime.now() + .seconds(CLightBulb.READ_VALUE_TIMEOUT)
timeoutResult = .success
results = CResults()
results.SetResult(code: CResults.code.success)
let dispatchGroup = DispatchGroup()
DispatchQueue.global(qos: .userInteractive).async
  {
  //let dispatchGroup = DispatchGroup()
  dispatchGroup.enter()
  characteristic.readValue(completionHandler:
    { (error) in
      if error != nil
        {
        results.SetResult(code: CResults.code.homeKitError)
        results.SetHomeKitDescription(text: error!.localizedDescription)
        print("Error in reading the brightness level for \(name): \(error!.localizedDescription)")
        }
       else
          {
          brightness = characteristic.value as! Int
          print("CLightBulb: -->Read the brightness level.  It is \(brightness) at " + Date().description(with: Locale.current))
          }
      dispatchGroup.leave()
    })
    timeoutResult = dispatchGroup.wait(timeout: timeout)
    if (timeoutResult == .timedOut)
      {
      results.SetResult(code: CResults.code.timedOut)
      }
     else
        {
        print("CLightBulb: (After wait) The brightness level is \(brightness) at " + Date().description(with: Locale.current))
        self.m_brightnessLevel = brightness
        }
  }
return(results)
}



Thank you!

1 个答案:

答案 0 :(得分:1)

如果您要使用自己的函数包装异步函数,通常最好也为包装函数提供完成处理程序。注意对完成处理程序的调用。这是您传递结果值的地方(即在闭包内):

func getBrightness(characteristic: HMCharacteristic, completion: @escaping (Int?, Error?) -> Void) {

    characteristic.readValue { (error) in

        //Program flows here second

        if error == nil {
            completion(characteristic.value as? Int, nil)
        } else {
            completion(nil, error)
        }
    }

    //Program flows here first
}

然后当你调用你的函数时,你只需要确保你在完成处理程序中处理结果(即关闭):

getBrightness(characteristic: characteristic) { (value, error) in

    //Program flows here second

    if error == nil {
        if let value = value {
            print(value)
        }
    } else {
        print("an error occurred: \(error.debugDescription)")
    }
}

//Program flows here first

始终记住代码将在异步函数完成之前流过。因此,您必须对代码进行结构化,以便根据返回的值或错误执行任何操作,并且在完成之前不会执行。