1

I am trying to create a concurrent/serial queue using the dispatch_queue_create and set it's priority to "HIGH" by setting a High priority queue as it's target queue. However, the result on console is unexpected.

Why exactly is this happening? Shouldn't my custom queue task be executed before Default and Low priority queues tasks?

-(void)callWebServiceUsername:(NSString *)username Password:(NSString*)password CompletionHandler:(void (^)(NSDictionary* dic, NSError *err))compHandler
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_DEFAULT - first");
    });

    dispatch_queue_t q = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);

    dispatch_async(q, ^{
        NSLog(@"DISPATCH_QUEUE_PRIORITY_HIGH - first");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_LOW - first");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_DEFAULT - second");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_BACKGROUND - first");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_LOW - second");
    });

    dispatch_queue_t queueConcurrent = dispatch_queue_create("MyCONCURRENTQueue", DISPATCH_QUEUE_CONCURRENT);
    __weak dispatch_queue_t weakqueueConcurrent = queueConcurrent;

    dispatch_async(weakqueueConcurrent, ^(void){
        const char *label = dispatch_queue_get_label(weakqueueConcurrent);
        NSString *str = [NSString stringWithUTF8String:label];
        NSLog(@"%@",str);
        dispatch_async(dispatch_get_main_queue(), ^{
            compHandler([[NSDictionary alloc] initWithObjects:[NSArray arrayWithObject:str] forKeys:[NSArray arrayWithObject:@"Label"]],NULL);
        });
    });

    dispatch_queue_t queueSerial = dispatch_queue_create("MySERIALQueue", DISPATCH_QUEUE_SERIAL);
    __weak dispatch_queue_t weakqueueSerial = queueSerial;

    dispatch_async(weakqueueSerial, ^(void){

        const char *label = dispatch_queue_get_label(weakqueueSerial);
        NSString *str = [NSString stringWithUTF8String:label];
        NSLog(@"%@",str);
        dispatch_async(dispatch_get_main_queue(), ^{
            compHandler([[NSDictionary alloc] initWithObjects:[NSArray arrayWithObject:str] forKeys:[NSArray arrayWithObject:@"Label"]],NULL);
        });
    });

    dispatch_set_target_queue(weakqueueSerial, q);

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_DEFAULT - third");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_BACKGROUND - second");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_HIGH - second");
    });

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
        NSLog(@"DISPATCH_QUEUE_PRIORITY_DEFAULT - fourth");
    });
}

The output on Console is:

2015-11-17 12:48:51.954 WebServiceCall[3745:98566] DISPATCH_QUEUE_PRIORITY_HIGH - second
2015-11-17 12:48:51.954 WebServiceCall[3745:98514] DISPATCH_QUEUE_PRIORITY_HIGH - first
2015-11-17 12:48:51.954 WebServiceCall[3745:98531] DISPATCH_QUEUE_PRIORITY_DEFAULT - first
2015-11-17 12:48:51.956 WebServiceCall[3745:98531] DISPATCH_QUEUE_PRIORITY_DEFAULT - third
2015-11-17 12:48:51.956 WebServiceCall[3745:98531] DISPATCH_QUEUE_PRIORITY_DEFAULT - fourth
2015-11-17 12:48:51.954 WebServiceCall[3745:98530] DISPATCH_QUEUE_PRIORITY_DEFAULT - second
2015-11-17 12:48:51.955 WebServiceCall[3745:98567] MyCONCURRENTQueue
2015-11-17 12:48:51.954 WebServiceCall[3745:98565] MySERIALQueue
2015-11-17 12:48:51.955 WebServiceCall[3745:98568] DISPATCH_QUEUE_PRIORITY_LOW - second
2015-11-17 12:48:51.954 WebServiceCall[3745:98532] DISPATCH_QUEUE_PRIORITY_LOW - first
2015-11-17 12:48:51.957 WebServiceCall[3745:98568] DISPATCH_QUEUE_PRIORITY_BACKGROUND - second
2015-11-17 12:48:51.957 WebServiceCall[3745:98531] DISPATCH_QUEUE_PRIORITY_BACKGROUND - first

Shouldn't the result be something like this:

2015-11-17 12:48:51.954 WebServiceCall[3745:98566] DISPATCH_QUEUE_PRIORITY_HIGH - second
    2015-11-17 12:48:51.954 WebServiceCall[3745:98514] DISPATCH_QUEUE_PRIORITY_HIGH - first
2015-11-17 12:48:51.954 WebServiceCall[3745:98565] MySERIALQueue
Shradha
  • 991
  • 3
  • 13
  • 38
  • 3
    You don't set the target queue until after submitting tasks to the queues, and you're wondering why it doesn't appear to have any effect? – Avi Nov 17 '15 at 07:49
  • oh.. I figured out the mistake now.. thank you.. – Shradha Nov 17 '15 at 08:12

0 Answers0