作者qnismine (qn是我的)
看板MacDev
標題[問題] 有關dispatch_async的問題請教
時間Thu Sep 10 11:43:40 2015
在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呢...拜託各位幫我解答了
--
作者 xoara (PURE) 看板 HatePolitics
標題 [新聞] 姚立明:柯P將以超過80萬票贏得北市長
時間 Sun Nov 9 19:39:20 2014
1F:推 winnerme: 本日最好笑就頒給這篇了XDDDDDDDDDDDDDDDDDDDDDDDDDDDDD11/09 19:48
2F:推 kech9111: 白痴 這些幕僚11/09 19:50
3F:推 kioh: 這智商,我快笑死了11/09 19:56
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.34.167.9
※ 文章網址: https://webptt.com/m.aspx?n=bbs/MacDev/M.1441856627.A.E12.html
※ 編輯: qnismine (1.34.167.9), 09/10/2015 11:44:19
4F:推 tom19830924: 我只有看文字部分 你UI有再main thread上更新嗎 09/10 13:44
dispatch_get_main_queue();
我有這在這dispatch_async的block setimage 和 stop spinner 可是都沒動作..
※ 編輯: qnismine (1.34.167.9), 09/10/2015 14:08:51
5F:→ darktt: 頻繁的呼叫NSNotificationCenter會有delay的問題,不怎麼 09/10 14:19
6F:→ darktt: 建議這麼做,可以改用block或delegate來解決 09/10 14:19
7F:→ darktt: 另外你的notification center怎麼是使用addObserver來當 09/10 14:21
8F:→ darktt: post資訊用? 09/10 14:21
9F:→ darktt: 我知道了,你的dispatch_async不該加在addObserver的時候 09/10 14:24
原來頻繁呼叫會有這個問題...我post資料是在別的類別下...解碼圖片後再post資料廣播
我在這裡寫的只有接收端而已...我之前是用nstimer來接 因為nstimer好像是獨立main
thread的...一直把image set到 imageview裡..只是久了偶爾會出現Bad Access,或delay
想說用別的方式來寫寫看 以前寫android的時候 是在一個message handler只要callback
回來就setimage到 imageview上 所以就用類似的nsnotificationcenter 做做看
不知道block怎麼寫一個callback 從別的類別能夠一直回傳資料到view controller上..
※ 編輯: qnismine (1.34.167.9), 09/10/2015 14:30:49
10F:→ darktt: 而是在post notification center的時候 09/10 14:25
11F:→ qnismine: !!!沒錯!! 的確是把main_queue加在post的地方.. 09/10 14:35
12F:→ qnismine: 不過的確會有效能的問題... 09/10 14:36
13F:推 liddle: 別這樣用NSNotificationCenter,看一下你的memory 09/10 17:25
14F:→ Esvent: 註冊NotificationCenter不一定要在main thread 09/11 00:28
15F:→ Esvent: 倒是你的notification在哪個thread post 就會在哪個thread 09/11 00:29
16F:→ Esvent: 被收到 所以重點是在post 09/11 00:30