What's the difference between the “global queue” and the “main queue” in GCD?

前端 未结 4 760
感动是毒
感动是毒 2020-12-12 14:22

Among some other ways, there are these two ways to get queues in GCD:

dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

dispatch_g         


        
4条回答
  •  难免孤独
    2020-12-12 14:51

    Yes. You can run code like this on a device to test it:

    dispatch_async(
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                NSLog(@"Block 1a");
                NSAssert(![NSThread isMainThread], @"Wrong thread!");
                NSLog(@"Block 1b");
            });
    dispatch_async(
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                dispatch_async(dispatch_get_main_queue(), ^{
                        NSLog(@"Block 2a");
                        NSAssert([NSThread isMainThread], @"Wrong thread!");
                        NSLog(@"Block 2b");
                    });
            });
    

提交回复
热议问题