如何在后台处理调度异步进程? [英] How to process dispatch async process in background?

查看:70
本文介绍了如何在后台处理调度异步进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用新的dispatch_queue接收Xmpp消息,同时更新了我的标签栏计数,即发送通知.但是更新我的Uitabbar计数需要花费更多时间.所以我单独使用dispatch_queue_main()调用通知过程.但这会使我的应用程序冻结几秒钟,同时更新我的​​标签栏计数.

  dispatch_queue_t exampleQueue = dispatch_queue_create("xmpp_message",NULL);dispatch_async(exampleQueue,^ {//用于处理消息的代码...dispatch_queue_t queue = dispatch_get_main_queue();dispatch_async(queue,^ {[self sendNotification:msg];});}); 

对此有任何帮助,可以在不冻结的情况下处理通知过程...

解决方案

上面的语法看起来不错,并采用了适当的技术将任务分派到后台进程,然后将UI更新重新分派回主队列.因此,您可能必须扩大调查范围.考虑到这一点,您可能要考虑:

  • 您完全确定在处理消息的代码"部分下没有与UI更新相关的代码吗?我见过人们报告无法解释的速度下降,然后说类似哦,我也不知道它也包括Core Graphics".我知道这不太可能,但是请仔细检查.

  • 这是一个愚蠢的问题,但是您是否在两个代码块的开头都放置了 NSLog 语句?通过这样做,您可以确认哪个队列是罪魁祸首(如果有的话),可以更好地了解队列的入口和出口等.不知道您的代码,我会担心用于处理消息的代码"花费的时间太长./p>

    所以您可以:

      dispatch_queue_t exampleQueue = dispatch_queue_create("xmpp_message",NULL);dispatch_async(exampleQueue,^ {NSLog(@%s已调度到xmpp_message",__ FUNCTION__);//用于处理消息的代码...dispatch_queue_t队列= dispatch_get_main_queue();dispatch_async(queue,^ {NSLog(@%s重新发送到主队列",__ FUNCTION__);[self sendNotification:msg];NSLog(@%s完成分发到主队列",__ FUNCTION__);});NSLog(@%s完成发送到xmpp_message",__ FUNCTION__);});//如果不是ARC或不支持6.0之前的iOS版本,则应释放队列dispatch_release(exampleQueue); 

  • 您可能还希望确保没有由于自定义队列的串行性而导致的问题.是否需要串行性质,还是可以考虑并发队列?

    所以尝试:

      dispatch_queue_t exampleQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);//或在iOS的最新版本中,可以使用dispatch_queue_create("xmpp_message",DISPATCH_QUEUE_CONCURRENT);dispatch_async(exampleQueue,^ {NSLog(@%s已调度到xmpp_message",__ FUNCTION__);//用于处理消息的代码...dispatch_queue_t队列= dispatch_get_main_queue();dispatch_async(queue,^ {NSLog(@%s重新发送到主队列",__ FUNCTION__);[self sendNotification:msg];});}); 

  • 最后,您可能想尝试使用构建并发用户界面上的WWDC 2012会话,以获取有关以下内容的演示:您如何使用该工具.

只有这些想法突然出现在我身上.

Iam using new dispatch_queue for receiving Xmpp messages , while updating my tabbar count iam sending the notification. but it takes more time for updating my Uitabbar count. so i call the notification process alone with dispatch_queue_main(). But it makes my app freeze for some seconds while updating my tabbar count..

dispatch_queue_t exampleQueue = dispatch_queue_create( "xmpp_message", NULL );
dispatch_async(exampleQueue, ^{
// code for proceesing messages....

 dispatch_queue_t queue=dispatch_get_main_queue();
dispatch_async(queue, ^{
    [self sendNotification:msg];
});
});

anyone help with this, to process the notification process without freezing...

解决方案

The above syntax looks fine, and employs the appropriate technique for dispatching tasks to a background process, but then re-dispatching the UI update back to the main queue. So, you probably have to broaden your investigation. With that in mind, you might want to consider:

  • Are you absolutely sure that no UI update related code slipped under the "code for processing messages" section? I've seen people report unexplained slow downs and then say something like "oh, I didn't know that included Core Graphics, too". I know this is unlikely, but check carefully.

  • It's a silly question, but have you put NSLog statements in here, right at the start of both blocks? By doing this, you can confirm which queue is the culprit (if either), better understand the entry and exits of queues, etc. Not knowing your code, I would worry that the "code for processing messages" is taking too long.

    So you might:

    dispatch_queue_t exampleQueue = dispatch_queue_create( "xmpp_message", NULL );
    dispatch_async(exampleQueue, ^{
    
        NSLog(@"%s dispatched to xmpp_message", __FUNCTION__);
    
        // code for processing messages....
    
        dispatch_queue_t queue = dispatch_get_main_queue();
        dispatch_async(queue, ^{
    
            NSLog(@"%s     re-dispatched to main queue", __FUNCTION__);
    
            [self sendNotification:msg];
    
            NSLog(@"%s     finished dispatch to main queue", __FUNCTION__);
        });
    
        NSLog(@"%s finished dispatched to xmpp_message", __FUNCTION__);
    });
    
    // if not ARC or supporting iOS versions prior to 6.0, you should release the queue
    
    dispatch_release(exampleQueue);
    

  • You might also also want to make sure that you're not having problems resulting from the serial nature of custom queues. Is the serial nature required, or can you contemplate concurrent queues?

    So try:

    dispatch_queue_t exampleQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // or in  recent versions of iOS, you can use dispatch_queue_create( "xmpp_message", DISPATCH_QUEUE_CONCURRENT );
    dispatch_async(exampleQueue, ^{
    
        NSLog(@"%s dispatched to xmpp_message", __FUNCTION__);
    
        // code for processing messages....
    
        dispatch_queue_t queue = dispatch_get_main_queue();
        dispatch_async(queue, ^{
    
            NSLog(@"%s re-dispatched to main queue", __FUNCTION__);
    
            [self sendNotification:msg];
        });
    });
    

  • You might, finally, want to try run app with the "Time Profiler" tool in Instruments. See the WWDC 2012 session on Building Concurrent User Interfaces for a demonstration of how you can use that tool.

Those are the only ideas that leap out at me.

这篇关于如何在后台处理调度异步进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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