sulabh qg
sulabh qg

Reputation: 1235

DispatchQueue.main when iOS app is in background mode?

What happend if URLSession sends request and before getting response iOS app goes into background mode, will self.myLabel.text get update?, will DispatchQueue.main work?

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()
}

Upvotes: 3

Views: 1555

Answers (2)

Sua Le
Sua Le

Reputation: 137

Best practice in this case, you must use weak self instead of strong self to avoid memory leak or safely access. And use DispatchQueue.main.async for code update UI only :)

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()
}

I have added [weak self] before your closure and update self?.myLabel?.text = ...

Following apple document, you should not update any view from background mode.

"Avoid updating your windows and views. Because your app’s windows and views are not visible when your app is in the background, you should avoid updating them. The exception is in cases where you need to update the contents of a window prior to having a snapshot of your app taken." Link: https://developer.apple.com/library/content/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

Upvotes: 1

Hitesh Sultaniya
Hitesh Sultaniya

Reputation: 939

You can do like this,

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()
}

I just updated this,

//Update
                 DispatchQueue.main.async {

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

                 }

Upvotes: 2

Related Questions