简体   繁体   English

dispatch_async(dispatch_get_main_queue(),^ {}); 从iOS7背景线程慢

[英]dispatch_async(dispatch_get_main_queue(), ^{}); from Background Thread slow on iOS7

I found a strange behavior on iOS7. 我在iOS7上发现了一个奇怪的行为。 I Have a dispatch_async background thread which handles heavy load and from this thread i want to update UI. 我有一个dispatch_async后台线程处理繁重的负载,我想要更新UI。 So i did just as follows: 所以我做了如下:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    [self heavyFunction];

    dispatch_async(dispatch_get_main_queue(), ^{

        [self updateUI];

    });
});

On iOS 8 and above the above code works as suggested but running on an iOS7 Test Device (iPhone 4s) the call to dispatch_async(dispatch_get_main_queue(), ^{}); 在iOS 8及以上版本中,上述代码按照建议工作,但在iOS7测试设备(iPhone 4s)上运行对dispatch_async的调用dispatch_async(dispatch_get_main_queue(), ^{}); takes 4 seconds everytime. 每次需要4秒钟。 I allready took the update and the heavy_function out and the same behavior still appears. 我已经完成了更新和重功能,同样的行为仍然出现。 Then i builded up a minimal example, in which it does not appear. 然后我建立了一个最小的例子,它没有出现。 So i thought maybe something else is blocking my main_queue on iOS7 but i can't figure out what this should be. 所以我想也许其他东西阻止我在iOS7上的main_queue,但我无法弄清楚这应该是什么。 There is no other UI related stuff happening on this ViewController until the [self heavyfunction]; [self heavyfunction];之前,这个ViewController上没有其他UI相关的东西发生[self heavyfunction]; finishes. 饰面。

Everything is compiled with XCode 7.3.1 with Base SDK 9.3 and Deployment Target 7.0. 所有内容都使用XCode 7.3.1与Base SDK 9.3和Deployment Target 7.0进行编译。

Does anyone had this behavior already and could point me in a direction what could be blocking here? 有没有人已经有过这种行为,可能会指出我可能阻止的方向吗?

So, after some research i found the problem. 因此,经过一些研究后我发现了这个问题。 I call the background thread with a notification (over the NSNotificationCenter ) from my backend and this notification get's catched from two different ViewControllers to update data sources for UITableViews. 我从后端通过后台线程调用通知(通过NSNotificationCenter ),并从两个不同的ViewController获取此通知以更新UITableViews的数据源。 The second one (the one with the hang) is in NavigationController hierarchy under the first one. 第二个(挂起的)位于第一个下面的NavigationController层次结构中。

Both notifications get handled the same time and the UI reloads on both controllers, even if the first one isn't visible at all. 两个通知都会在同一时间处理,并且UI会在两个控制器上重新加载,即使第一个通知根本不可见。 But this just seem to happen on iOS 7. All above iOS versions just don't reload the UI from the invisible ViewController it seems. 但这似乎恰好发生在iOS 7上。所有上述iOS版本都没有从看似无形的ViewController重新加载UI。

When i jump between ViewControllers everything on the UI get's updated either way, because the data source gets updated from the background thread, so i solved the problem by just checking which of the booth ViewControllers is shown right now and only reload the UI on that one. 当我在ViewControllers之间跳转时,UI上的所有内容都以任何方式更新,因为数据源从后台线程更新,所以我通过检查现在显示哪个展位ViewControllers并仅重新加载UI来解决问题。

What still bugs me is that all iOS versions above iOS 7 seem to handle this problem automatically, but iOS 7 doesn't. 仍然让我感到困惑的是,iOS 7以上的所有iOS版本似乎都会自动处理这个问题,但iOS 7却没有。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在UI线程上运行dispatch_async(dispatch_get_main_queue()的目的 - Purpose of running dispatch_async(dispatch_get_main_queue() on UI Thread 了解ios UIView动画块和dispatch_async(dispatch_get_main_queue)块 - Understanding ios UIView animation block and dispatch_async(dispatch_get_main_queue) block 从xctest执行dispatch_async(dispatch_get_main_queue() - Execute dispatch_async(dispatch_get_main_queue() from xctest 调用dispatch_async(dispatch_get_main_queue())时UIView屏幕冻结 - UIView screen freeze when calling dispatch_async(dispatch_get_main_queue()) 在什么情况下dispatch_async(dispatch_get_main_queue(),^ ...什么都不做 - Under what circumstances does dispatch_async(dispatch_get_main_queue(), ^ … do nothing 使用“ dispatch_async(dispatch_get_main_queue(),^ {block})更新UI - Updating the UI using "dispatch_async(dispatch_get_main_queue(),^{block}) NSMutableURLRequest超时后未触发dispatch_async(dispatch_get_main_queue() - dispatch_async(dispatch_get_main_queue() not firing after NSMutableURLRequest with timeoutInterval 嵌套dispatch_async(dispatch_get_main_queue()^ {})的目的是什么? - What is the purpose of nesting dispatch_async(dispatch_get_main_queue()^{}) ? 使用dispatch_async + dispatch_get_main_queue进行UI更新有什么意义? - What's the point in using dispatch_async + dispatch_get_main_queue for UI updates? 直接执行方法与在dispatch_async(dispatch_get_main_queue(),^ {})块中有什么区别 - What is the difference between perform a method directly and in the block of dispatch_async(dispatch_get_main_queue(), ^{})
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM