2012-11-29 65 views
0

Iam使用新的dispatch_queue接收Xmpp消息,同时更新我的​​tabbar计数并发送通知。但它需要更多时间来更新我的Uitabbar计数。所以我用dispatch_queue_main()单独调用通知进程。但它使我的应用程序冻结几秒钟,同时更新我的​​TabBar算..如何在后台处理调度异步进程?

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]; 
}); 
}); 

任何这方面的帮助,不结冰处理通知过程...

回答

3

上述语法看起来不错,并采用将任务分派到后台进程的适当技术,然后将UI更新重新分派回主队列。所以,你可能不得不扩大你的调查范围。考虑到这一点,你可能要考虑:

  • 绝对确保没有UI更新相关的代码部分之下“处理消息代码”下滑?我看到有人报告说不明原因的缓慢起伏,然后说出类似“哦,我也不知道那里也包括了Core Graphics”。我知道这不太可能,但仔细检查。

  • 这是一个愚蠢的问题,但你有没有把NSLog陈述在这里,在两个块的开始?通过这样做,你可以确认哪个队列是罪魁祸首(如果有的话),更好地理解队列的进入和退出等。不知道你的代码,我担心“处理消息的代码”耗时过长。所以

    可能会:

    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); 
    
  • 你也可能还需要确保你没有从定制队列的串行特性造成的问题。是否需要串行性质,还是可以考虑并发队列?

    所以尝试:

    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]; 
        }); 
    }); 
    
  • 你可能会,最后,想尝试与Instruments “时间探查器” 工具中运行的应用程序。有关如何使用该工具的演示,请参见Building Concurrent User Interfaces上的WWDC 2012会话。

那些是唯一跳出我的想法。

+0

我已经尝试过第二个代码的开始,同样的减速发生......当我切换到主队列的全局队列,小冻结发生在更新uitabbar计数..如何控制冻结,如果我不切换到主队列,没有冻结发生,但tabbarcount需要更多时间来更改...希望你能帮助我..与此..... –

+0

是否有任何其他方法除主队列,使UItabbar快速计数变化而不会冻结... –

+0

@RahulNair所以,“重新发送”的消息很快显示出来,并且标签栏缓慢变化?或者“重新发布”的“NSLog”消息也显示缓慢? – Rob