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

iOS Xcode :
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.

Reason: call UI update in a thread which is not the main thread.

Solution:
Swift:
dispatch_async(dispatch_get_main_queue(), {
let vc = SomeViewController()
self.presentViewController(vc, animated: true, completion: nil)
})

Objective-C:
dispatch_async(dispatch_get_main_queue(), ^{
[self presentViewController:someViewController animated:YES completion:nil];
});

OR
Do not change UI from anything but the main thread. While it may appear to work on some OS or devices and not others, it is bound to make your application unstable, and crash unpredictably.

If you must respond to a notification, which can happen in the background, then ensure UIKit invocation takes place on the main thread.

You at least have these 2 options:

Asynchronous Dispatch

Use GCD (Grand Central Dispatch) if your observer can be notified on any thread. You can listen and do work from any thread, and encapsulate UI changes in a dispatch_async:

dispatch_async(dispatch_get_main_queue()) {
// Do UI stuff here
}
When to use GCD? When you do not control who sends the notification. It can be the OS, a Cocoapod, embedded libraries, etc. Using GCD will woke anytime, every time. Downside: You find yourself re-scheduling the work.

Listen on Main Thread

Conveniently, you can specify on which thread you want the observer to be notified, at the time you are registering for notifications, using the queue parameter:

addObserverForName:@”notification”
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *note){
// Do UI stuff here
}
When to observe on main thread? When you are both registering and registered. Bu the time you respond to the notification, you are already where you need to be.

Post Notification On Main Thread

[self performSelectorOnMainThread:@selector(postNotification:) withObject:notification waitUntilDone:NO];
Hybrid solution which does not guarantee that the observer is only invoked from said method. It allows for lighter observer, at the cost less robust design. Only mentioned here as a solution you should probably avoid.

http://stackoverflow.com/questions/31951704/this-application-is-modifying-the-autolayout-engine-from-a-background-thread-wh

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值