当iOS应用程序处于后台模式时,DispatchQueue.main?

时间:2017-12-13 09:50:21

标签: swift multithreading grand-central-dispatch

如果URLSession发送请求并且在获得响应iOS应用程序进入后台模式之前会发生什么,self.myLabel.text会更新吗?,DispatchQueue.main会工作吗?

func updateUILabel(callback :(NSDictionary?) -> Void ){


    // create post request
    let url = URL(string: "https:anyserver.xyz”)!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        DispatchQueue.main.async({
              guard let data = data, error == nil else {
                print(error?.localizedDescription ?? "No data")
              }

              let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
              if let responseJSON = responseJSON as? [String: Any] {
                 self.myLabel.text = responseJSON[“text”] as! String
              }
       })
    }

    task.resume()
}

2 个答案:

答案 0 :(得分:2)

你可以这样做,

func updateUILabel(callback :(NSDictionary?) -> Void ){


    // create post request
    let url = URL(string: "https:anyserver.xyz”)!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        DispatchQueue.main.async({
              guard let data = data, error == nil else {
                print(error?.localizedDescription ?? "No data")
              }

              let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
              if let responseJSON = responseJSON as? [String: Any] {
                //Update
                 DispatchQueue.main.async {

                       self.myLabel.text = responseJSON[“text”] as! String                         

                 }

              }
       })
    }

    task.resume()
}

我刚刚更新了这个,

//Update
                 DispatchQueue.main.async {

                       self.myLabel.text = responseJSON[“text”] as! String                         

                 }

答案 1 :(得分:1)

在这种情况下的最佳做法是,您必须使用弱自我而不是强自我来避免内存泄漏或安全访问。 并仅使用DispatchQueue.main.async进行代码更新UI:)

func updateUILabel(callback :(NSDictionary?) -> Void ){


    // create post request
    let url = URL(string: "https:anyserver.xyz”)!
    var request = URLRequest(url: url)
    request.httpMethod = "POST"

    let task = URLSession.shared.dataTask(with: request) {[weak self] data, response, error in
           guard let data = data, error == nil else {
              print(error?.localizedDescription ?? "No data")
              return
           }
           let responseJSON = try? JSONSerialization.jsonObject(with: data, options: [])
              if let responseJSON = responseJSON as? [String: Any] {
                //Update
                 DispatchQueue.main.async {

                       self?.myLabel?.text = responseJSON[“text”] as? String                         

                 }

              }
       })
    }

    task.resume()
}

我在关闭之前添加了 [弱自我] 并更新 self?.myLabel?.text = ......

关注Apple文档,您不应该从后台模式更新任何视图。

" 避免更新您的窗口和视图。由于当您的应用在后台时,您的应用的窗口和视图不可见,因此您应该避免更新它们。例外情况是您需要在拍摄应用程序快照之前更新窗口内容。" 链接:https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html