dispatch_async已经滞后了,找不到哪里。有NSLog问题吗? [英] dispatch_async has lag somewhere, can't find where. is there an NSLog problem?

查看:118
本文介绍了dispatch_async已经滞后了,找不到哪里。有NSLog问题吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{

//Bunch of code

NSLog(@"Test");

});

运行并立即返回nslog。但是代码的结果只会在屏幕上出现几秒延迟。在这里使用nslog是否存在问题,这意味着它通常会在它之前被调用,这使得它看起来很快,而事实并非如此。我很困惑这个延迟来自哪里,因为NSLog正好在所有运行的代码的末尾。

which runs and returns nslog immediately. But the results of the code only appear on the screen a few seconds delay. Is there a problem with nslog being used here, which means it's called before it would usually, making it seem speedy when really it isn't. I'm confused as to where this delay is coming from, as the NSLog is right at the end of all the code which runs then.

另外,我的另一个解决方案问题,是否有可能在调用每个方法时得到一个NSLog(有点像我想的NSZombiesEnabled)所以我可以确保没有一点我没注意到我的应用程序的甜蜜响应时间? / p>

Also, another solution to my problem, is it possible to get an NSLog when every method is called (a bit like NSZombiesEnabled i suppose) so i can make sure there isn't some bit which i didn't notice taking up the sweet response time of my app?

推荐答案

UIKit不是线程安全的。你做的任何影响你的UI的调用需要在主线程上进行,否则你会得到奇怪的,不可预测的行为。示例:

UIKit isn’t thread-safe. Any calls you make that affect your UI need to be made on the main thread, or you’ll get weird, unpredictable behavior. Example:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

    // Background code that doesn't need to update the UI here

    dispatch_async(dispatch_get_main_queue(), ^{
        // UI code here
    });
});

这篇关于dispatch_async已经滞后了,找不到哪里。有NSLog问题吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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