热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

IOS9输出UIK的提示存在崩溃问题的风险

最近做项目升级了Xcode到7.1.1,突然出现了这个问题,而且容易导致程序崩溃,经过查询顺利解决,记录如下;1:问题:Thisapplicationismodifyingthea

最近做项目升级了Xcode到7.1.1,突然出现了这个问题,而且容易导致程序崩溃,经过查询顺利解决,记录如下;

1:问题: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.

2:只需要把UI的崩溃的部分添加到dispatch_async(dispatch_get_main_queue(), ^(void){ });内部即可;


相关资料如下:

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

up vote 9 down vote favorite
4

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?

share improve this question
 

1 Answer

active oldest votes
up vote 19 down vote accepted

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

share improve this answer
 
 
so we do make a couple of updates on the UI when we hear a notification, so are you saying that I need to make those updates inside "dispatch_async(dispatch_get_main_queue()"? –  Shabarinath Pabba  Aug 11 at 21:16
 
Exactly. I have added examples. –  SwiftArchitect  Aug 11 at 21:24
 
alrighty, trying this now and will choose yours as an answer if it fixed it :D –  Shabarinath Pabba  Aug 11 at 21:27
 
why was this not required in iOS 8?, why does this happen in iOS 9? –  Shabarinath Pabba  Aug 11 at 21:29
1  
It was best practice well before iOS 8 - Apple has just chosen iOS 9 to start warning of improper updates, and, as the error states, will soon being disallowing UI updates on a background thread entirely. –  Scott Austin  Sep 17 at 15:02




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










推荐阅读
author-avatar
手机用户2502924457
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有