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

看板MacDev作者 (qn是我的)時間9年前 (2015/09/10 11:43), 9年前編輯推噓2(2011)
留言13則, 5人參與, 最新討論串1/1
在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

11/09 19:48,
本日最好笑就頒給這篇了XDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
11/09 19:48

11/09 19:50,
白痴 這些幕僚
11/09 19:50

11/09 19:56,
這智商,我快笑死了
11/09 19:56
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.34.167.9 ※ 文章網址: https://www.ptt.cc/bbs/MacDev/M.1441856627.A.E12.html ※ 編輯: qnismine (1.34.167.9), 09/10/2015 11:44:19

09/10 13:44, , 1F
我只有看文字部分 你UI有再main thread上更新嗎
09/10 13:44, 1F
dispatch_get_main_queue(); 我有這在這dispatch_async的block setimage 和 stop spinner 可是都沒動作.. ※ 編輯: qnismine (1.34.167.9), 09/10/2015 14:08:51

09/10 14:19, , 2F
頻繁的呼叫NSNotificationCenter會有delay的問題,不怎麼
09/10 14:19, 2F

09/10 14:19, , 3F
建議這麼做,可以改用block或delegate來解決
09/10 14:19, 3F

09/10 14:21, , 4F
另外你的notification center怎麼是使用addObserver來當
09/10 14:21, 4F

09/10 14:21, , 5F
post資訊用?
09/10 14:21, 5F

09/10 14:24, , 6F
我知道了,你的dispatch_async不該加在addObserver的時候
09/10 14:24, 6F
原來頻繁呼叫會有這個問題...我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

09/10 14:25, , 7F
而是在post notification center的時候
09/10 14:25, 7F

09/10 14:35, , 8F
!!!沒錯!! 的確是把main_queue加在post的地方..
09/10 14:35, 8F

09/10 14:36, , 9F
不過的確會有效能的問題...
09/10 14:36, 9F

09/10 17:25, , 10F
別這樣用NSNotificationCenter,看一下你的memory
09/10 17:25, 10F

09/11 00:28, , 11F
註冊NotificationCenter不一定要在main thread
09/11 00:28, 11F

09/11 00:29, , 12F
倒是你的notification在哪個thread post 就會在哪個thread
09/11 00:29, 12F

09/11 00:30, , 13F
被收到 所以重點是在post
09/11 00:30, 13F
文章代碼(AID): #1LyFnpuI (MacDev)
文章代碼(AID): #1LyFnpuI (MacDev)