2012-11-29 132 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