此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏 [英] 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()
}
推荐答案
dataTaskWithRequest
调用在后台运行,然后从同一线程调用完成处理程序.任何更新UI的操作都应在主线程上运行,因此所有当前的处理程序代码应在dispatch_async
之内,并返回主队列:
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
}
快捷键3:
DispatchQueue.main.async() {
// Do stuff to UI
}
因此,理想情况下,当前在if error == nil
中拥有的所有代码都应该在另一个函数(称为handleRequest)中关闭,这样您的当前代码将变为:
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
})
}
这篇关于此应用程序正在从后台线程修改自动布局引擎,这可能导致引擎损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!