2

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?

volperossa
  • 1,339
  • 20
  • 33
  • 2
    please post the proper code. You are missing a bracket and a semicolon – Teja Nandamuri Oct 02 '15 at 15:41
  • 1
    Look into [`dispatch_after()`](https://developer.apple.com/library/mac/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html#//apple_ref/c/func/dispatch_after). – i_am_jorf Oct 02 '15 at 16:13
  • When it freezes, pause the app in the debugger and look at what's happening on the main thread at that time. – Phillip Mills Oct 02 '15 at 17:16

1 Answers1

2

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.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
  • O_O so.. you're telling me that my new queue could run in main thread??How can avoid it? – volperossa Oct 03 '15 at 08:51
  • i ckecked...there are 2 different thread and the code is executed correnctly in two different queue..but it freezes also when it is executing in different thread – volperossa Oct 03 '15 at 12:54