Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

This application is modifying the autolayout engine from a background thread, which can lead to engine corruption

I am getting this error This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.This will cause an exception in a future release. I don't know what is causing this error. Can anybody help me.

func getUserDataFromTwitterWithUser(user : PFUser)
 {
//NRLoader.showLoader()
let strTwURL = "https://api.twitter.com/1.1/users/show.json?     screen_name="+PFTwitterUtils.twitter()!.screenName! + "&access_token="+PFTwitterUtils.twitter()!.authToken!
let twURL = NSURL (string: strTwURL)

let request = NSMutableURLRequest(URL: twURL!, cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy, timeoutInterval: 2.0) as NSMutableURLRequest

PFTwitterUtils.twitter()?.signRequest(request)

let session = NSURLSession.sharedSession()

session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
  if error == nil {
    var  jsonOptional = Dictionary<String, AnyObject>()

    do {
      jsonOptional = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! Dictionary<String, AnyObject>
      // use jsonData
    } catch {
      // report error
    }
    var userName = ""
    if let screenName = jsonOptional["screen_name"] as? String{
      userName = screenName
    }
    else if let name = jsonOptional["name"] as? String{
      userName = name
    }

    var profilePicUrl = ""


    if let picUrl = jsonOptional["profile_image_url"] as? String{
      profilePicUrl = picUrl
    }
    AppUser.currentUser()?.username = userName
    AppUser.currentUser()?.profileAwsURL = profilePicUrl
    //NRLoader.hideLoader()
    //if ParseUtils.isLoggedInUserIsAnonymous() {
      let signUpVC:SignMeUpViewController = self.storyboard!.instantiateViewControllerWithIdentifier("SignMeUpViewController") as! SignMeUpViewController
      signUpVC.isFromLogin = true
      self.navigationController!.pushViewController(signUpVC, animated: true)

    //} else {
     // self.pushToSubmitDreamViewController()
    //}
  }
  else {
    //NRLoader.hideLoader()
    NRToast.showToastWithMessage(error!.description)
  }


}).resume()
 }
like image 417
Ganesh Kumar Avatar asked Feb 24 '16 05:02

Ganesh Kumar


1 Answers

The dataTaskWithRequest call runs in the background and then calls your completion handler from the same thread. Anything that updates the UI should run on the main thread, so all of your current handler code should be within a dispatch_async back onto the main queue:

dispatch_async(dispatch_get_main_queue()) {
  // Do stuff to UI
}

Swift 3:

DispatchQueue.main.async() {
  // Do stuff to UI
}

Therefore, ideally all the code you currently have within if error == nil should be off in another function, say called handleRequest, so your current code becomes:

session.dataTaskWithRequest(request, completionHandler: {(data, response, error) in
    if error == nil {
        dispatch_async(dispatch_get_main_queue(), {
            self.handleRequest(...)I
        })
    }
like image 89
Michael Avatar answered Sep 21 '22 10:09

Michael