[問題] 有關dispatch_async的問題請教

作者: qnismine (qn是我的)   2015-09-10 11:43:40
在viewdidload裡面我加了兩條Thread
[NSThread detachNewThreadSelector:@selector(gotortsp) toTarget:self
withObject:nil];
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_async(mainQueue, ^{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(getUserProfileSuccess:)
name:@"Notification_GetUserProfileSuccess" object:nil]; });
-(void)gotortsp
{
[self runrtsp];
}
- (void) getUserProfileSuccess: (NSNotification*) aNotification
{
NSLog(@"in");
[self.spinner stopAnimating];
self.DisplaytoImage = [aNotification object];
}
由於runrtsp是建立一個live555連線一直傳封包進來讓ffmpeg解碼 解完碼再藉由
notification回傳到這裏的imageview update ui
我把回傳的寫在dispatch_get_main_queue();
為什麼沒辦法更新ui呢@@
可是log卻有印in的訊息 代表他有進來 但是卻沒有更新ui
由於runrtsp是個一直持續不間斷的收封包 所以只能做callback回來更新ui
因為不間斷 所以無法使用dispatch_queue_create()會一直卡在裡面...
應該要怎麼做才能夠確實的更新ui呢...拜託各位幫我解答了
作者: tom19830924 (Tom Hsu)   2015-09-10 13:44:00
我只有看文字部分 你UI有再main thread上更新嗎
作者: darktt (小朱)   2015-09-10 14:19:00
頻繁的呼叫NSNotificationCenter會有delay的問題,不怎麼建議這麼做,可以改用block或delegate來解決另外你的notification center怎麼是使用addObserver來當post資訊用?我知道了,你的dispatch_async不該加在addObserver的時候而是在post notification center的時候
作者: qnismine (qn是我的)   2015-09-10 14:35:00
!!!沒錯!! 的確是把main_queue加在post的地方..不過的確會有效能的問題...
作者: liddle (Guderian)   2015-09-10 17:25:00
別這樣用NSNotificationCenter,看一下你的memory
作者: Esvent (Esvent)   2015-09-11 00:28:00
註冊NotificationCenter不一定要在main thread倒是你的notification在哪個thread post 就會在哪個thread被收到 所以重點是在post

Links booklink

Contact Us: admin [ a t ] ucptt.com