This application is modifying the autolayout engine from a background thread

I am working with iOS 9 and Xcode 7. My app crashes when I build and run it. I get the following error. I have no idea how I am modifying the autolayout engine on a background thread. I may not be providing all of the neccessary info but I am not sure what someone would need to see in order to help solve the issue.


I do know that this is called six times and I do have six of the same type of view controller.


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.

Stack:(

0 CoreFoundation 0x000000010fb8bca5 __exceptionPreprocess + 165

1 libobjc.A.dylib 0x000000010f604dcd objc_exception_throw + 48

2 CoreFoundation 0x000000010fb8bbdd +[NSException raise:format:] + 205

3 Foundation 0x000000010f37f7cf _AssertAutolayoutOnMainThreadOnly + 79

4 Foundation 0x000000010f1e227e -[NSISEngine withBehaviors:performModifications:] + 31

5 UIKit 0x000000011098a304 -[UIView(AdditionalLayoutSupport) _withAutomaticEngineOptimizationDisabledIfEngineExists:] + 58

6 UIKit 0x000000011098ae43 -[UIView(AdditionalLayoutSupport) updateConstraintsIfNeeded] + 249

7 UIKit 0x000000011098b90a -[UIView(AdditionalLayoutSupport) _updateConstraintsAtEngineLevelIfNeeded] + 348

8 UIKit 0x00000001101f6b3a -[UIView(Hierarchy) _updateConstraintsAsNecessaryAndApplyLayoutFromEngine] + 159

9 UIKit 0x00000001102053c3 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 662

10 QuartzCore 0x000000010e85b044 -[CALayer layoutSublayers] + 150

11 QuartzCore 0x000000010e84fa44 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366

12 QuartzCore 0x000000010e84f8c2 _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24

13 QuartzCore 0x000000010e84448d _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277

14 QuartzCore 0x000000010e8713b8 _ZN2CA11Transaction6commitEv + 508

15 QuartzCore 0x000000010e87169a _ZN2CA11Transaction14release_threadEPv + 224

16 libsystem_pthread.dylib 0x000000011306f72a _pthread_tsd_cleanup + 86

17 libsystem_pthread.dylib 0x000000011306f451 _pthread_exit + 117

18 libsystem_pthread.dylib 0x000000011306e6cd _pthread_wqthread + 879

19 libsystem_pthread.dylib 0x000000011306c40d start_wqthread + 13

)


Take care,


Jon

Replies

I am having the same issue. Did you find a solution?

It could be something as simple as setting a text field value or adding a subview inside a background thread, which may cause a field's layout to change. Make sure anything you do with the interface only happens in the main thread.

Hey!
If u use UITableView try to add UITableViewDelegate in your @interface

Only need to either wrap those methods that call UI updates with dispatch_asynch get main queue or inside those methodes functions that update UI.

XCode 7 and iOS 9 SDK because very strict, compared to previous versions, and that is good. I found many such mistakes in my project that was compiling before perfectly.

Under Xcode 7.1 I'm suddenly seeing this same error when calling...


[PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus authorizationStatus)


It doesn't make sense!

dont know iof this is coming a bit late. I got the same message when changing from sendAsynchronousRequest to dataTaskWithRequest.

In my code I was setting up the contents of a UIImage.

I guess that is what this message means, i.e. that the code is changing the UI in a background thread.

But if you do not do it this way, how else are you supposed to update a UIImage?

The only other alternative would be a synchronous request and wait for it ???

Please start your own thread.

This only happens to me when using iPad Retina or iPad 2.

When selecting a newer model like iPad like Air, Air 2 or Pro I don't get the error.

I have the same issue in my swift programming code.


func backgroundThread(delay: Double = 0.0, background: (() -> Void)? = nil, completion: (() -> Void)? = nil) {

dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.rawValue), 0)) {

if(background != nil){ background!(); }

let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC)))

dispatch_after(popTime, dispatch_get_main_queue()) {

if(completion != nil){ completion!(); }

}

}

}


backgroundThread(background: {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {

self.fetchfeeder((url?.URL)!, callback: self.callbackData)

});

},

completion: {

print("Finish run background")

/

})



func fetchfeeder(url2: NSURL, callback:(String, String?)->Void) {

let request2=NSURLRequest(URL: url2)

let session2 = NSURLSession.sharedSession()

let task2 = session2.dataTaskWithRequest(request2) {

(data, header, error) -> Void in

if error != nil {

callback("",error!.localizedDescription)

} else {

let result=NSString(data: data!, encoding: NSUTF8StringEncoding)!

callback(result as String,nil)

}

}

task2.resume()

}



func callbackData(result:String, error:String?) {

if(error == nil){

let data=result.dataUsingEncoding(NSUTF8StringEncoding)

do {

menu = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.MutableContainers) as? NSMutableArray

mytable.reloadData()

activebar.hidden=true

} catch {

print("** Error json ***");

activebar.hidden=true

}

}

activebar.hidden=true

}

You have to interpret the error message as meaning "you tried to change the UI from a background thread". That will typically trigger autolayout, producing the actual message you saw.


What is 'activebar'? It sounds like a UI element, so you cannot set its "hidden" property from a background thread. Also, you should not send 'reloadData' to a table view on a background thread.

Its your main thread is conflicted or any other process is interrupting main thread so please call your method in side grand dispatch


dispatch_async(dispatch_get_main_queue()) {


}


This can solve your problem