此应用程序正在从后台线程修改autolayout引擎,这可能导致引擎损坏和奇怪的崩溃 [英] This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes

查看:120
本文介绍了此应用程序正在从后台线程修改autolayout引擎,这可能导致引擎损坏和奇怪的崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在模拟器中运行我的应用程序时,我在控制台中获取此日志。在iOS 8中没有看到这个。我不太清楚是什么导致了这一点。有没有其他人遇到同样的问题,如果是这样,它是如何解决的?或者是否有人可以提供任何帮助?

I get this log in the console when I am running my application in is simulator. Haven't seen this in iOS 8. I am not quite sure whats causing this. Has anyone else come across the same issue and if so how was it fixed? or is there any help anyone can provide in regards to this?

推荐答案

除了主线程之外,不要更改UI。虽然它似乎可以在某些操作系统或设备上运行而不是其他操作系统,但它必然会使您的应用程序不稳定,并且无法预测崩溃。

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.

如果您必须回复通知,可以在后台发生,然后确保 UIKit 调用发生在主线程上

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

您至少有这两个选项:

如果可以在任何线程上通知您的观察者,请使用 GCD (Grand Central Dispatch)。您可以从任何线程监听并执行工作,并在 dispatch_async 中封装UI更改:

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
}

何时使用 GCD ?当您不控制谁发送通知时。它可以是操作系统,Cocoapod,嵌入式库等。每次使用 GCD 都会随时醒来。缺点:您发现自己正在重新安排工作。

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.

方便地,您可以使用队列在您注册时发送通知 c $ c>参数:

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.

[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.

这篇关于此应用程序正在从后台线程修改autolayout引擎,这可能导致引擎损坏和奇怪的崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆