Objective-C freezed GUI also with queue

痞子三分冷 提交于 2019-12-24 04:21:42

问题


I'm trying to understand queue in iOS; with this code

dispatch_queue_t coda_thread=dispatch_queue_create("coda_thread",NULL);

//UIPROGRESS VIEW
for(i=0;i<=10;i=i+1)
{    
dispatch_async(coda_thread,
    ^{
        NSLog(@"CODA_THREAD");
        NSLog(@"attendo..");
        [NSThread sleepForTimeInterval:10];
        dispatch_async(dispatch_get_main_queue(),
        ^{
            NSLog(@"MAIN THREAD");
            NSLog(@"aggiorno barra.."); 
            [self.upv setProgress:i/10 animated:YES];

        });
    });

}  

I expected no freeze in GUI because sleep is in coda_thread (and not in main queue where is updated the GUI) queue while setProgress in main queue.. Instead I have freeze in my GUI..why this?


回答1:


The problem is that a dispatch queue is not a new thread. You have no guarantee that the dispatch queue is actually using a different thread. Combining GCD API with thread API just won't work.



来源:https://stackoverflow.com/questions/32910632/objective-c-freezed-gui-also-with-queue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!