6.10 Grouping Tasks Together with GCD

时间:2021-05-18 05:17:48


给任务分组,并确保他们一个挨着一个执行

使用 dispatch_group_create 来创建分组

假设有如下三个方法,我们希望他们一个挨着一个执行

  • - (void) reloadTableView{
  •     /* Reload the table view here */
  •     [NSThread sleepForTimeInterval:3];
  •     NSLog(@"%s", __FUNCTION__);
  •     NSLog(@"%@",[NSThread currentThread]);
  • }
  • - (void) reloadScrollView{
  •     /* Do the work here */
  •     [NSThread sleepForTimeInterval:2];
  •     NSLog(@"%s", __FUNCTION__);
  •     NSLog(@"%@",[NSThread currentThread]);
  • }
  • - (void) reloadImageView{
  •     /* Reload the image view here */
  •     [NSThread sleepForTimeInterval:1];
  •     NSLog(@"%s", __FUNCTION__);
  •     NSLog(@"%@",[NSThread currentThread]);
  • }
  • -(void)test6_10
  • {
  •     NSLog(@"%s", __FUNCTION__);
  •     NSLog(@"%@",[NSThread currentThread]);
  •     dispatch_group_t taskGroup = dispatch_group_create();
  •     dispatch_queue_t mainQueue = dispatch_get_main_queue();
  • //    dispatch_queue_t mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  •    
  •     /* Reload the table view on the main queue */
  •     dispatch_group_async(taskGroup, mainQueue, ^{
  •         [self reloadTableView];
  •     });
  •     /* Reload the scroll view on the main queue */
  •     dispatch_group_async(taskGroup, mainQueue, ^{
  •         [self reloadScrollView];
  •     });
  •     /* Reload the image view on the main queue */
  •     dispatch_group_async(taskGroup, mainQueue, ^{
  •         [self reloadImageView];
  •     });
  •    
  •    
  • //    dispatch_async(mainQueue, ^{
  • //        [self reloadTableView];
  • //    });
  • //    dispatch_async(mainQueue, ^{
  • //        [self reloadScrollView];
  • //    });
  • //    dispatch_async(mainQueue, ^{
  • //        [self reloadImageView];
  • //    });
  • //    dispatch_async(dispatch_get_main_queue(), ^{
  • //        
  • //        dispatch_sync(mainQueue, ^{
  • //            [self reloadTableView];
  • //        });
  • //        dispatch_sync(mainQueue, ^{
  • //            [self reloadScrollView];
  • //        });
  • //        dispatch_sync(mainQueue, ^{
  • //            [self reloadImageView];
  • //        });
  • //    });
  •    
  •    
  •    
  •     /* At the end when we are done, dispatch the following block */
  •     dispatch_group_notify(taskGroup, mainQueue, ^{
  •         /* Do some processing here */
  •         NSLog(@"%s", __FUNCTION__);
  •         NSLog(@"%@",[NSThread currentThread]);
  • //        [[[UIAlertView alloc] initWithTitle:@"Finished"
  • //                                    message:@"All tasks are finished"
  • //                                   delegate:nil
  • //                          cancelButtonTitle:@"OK"
  • //                          otherButtonTitles:nil, nil]
  • //         show];
  •        
  •     });
  •     /* We are done with the group */
  • //    dispatch_release(taskGroup);//ARC之后不允许了
  •    
  •     NSLog(@"%s will over", __FUNCTION__);
  •     NSLog(@"%@",[NSThread currentThread]);
  • }

结果是:


2014-03-07 11:02:14.943 cookbook[748:a0b] -[AppDelegate test6_10]

2014-03-07 11:02:14.944 cookbook[748:a0b] <NSThread: 0x8926400>{name = (null), num = 1}

2014-03-07 11:02:14.944 cookbook[748:a0b] -[AppDelegate test6_10] will over

2014-03-07 11:02:14.945 cookbook[748:a0b] <NSThread: 0x8926400>{name = (null), num = 1}

2014-03-07 11:02:17.970 cookbook[748:a0b] -[AppDelegate reloadTableView]

2014-03-07 11:02:17.971 cookbook[748:a0b] <NSThread: 0x8926400>{name = (null), num = 1}

2014-03-07 11:02:19.974 cookbook[748:a0b] -[AppDelegate reloadScrollView]

2014-03-07 11:02:19.975 cookbook[748:a0b] <NSThread: 0x8926400>{name = (null), num = 1}

2014-03-07 11:02:20.977 cookbook[748:a0b] -[AppDelegate reloadImageView]

2014-03-07 11:02:20.978 cookbook[748:a0b] <NSThread: 0x8926400>{name = (null), num = 1}

2014-03-07 11:02:20.991 cookbook[748:a0b] __23-[AppDelegate test6_10]_block_invoke34

2014-03-07 11:02:20.991 cookbook[748:a0b] <NSThread: 0x8926400>{name = (null), num = 1}



顺便说一下

如果把那一句改成这样


//    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    dispatch_queue_t mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);


结果是:

2014-03-07 11:04:48.075 cookbook[767:a0b] -[AppDelegate test6_10]

2014-03-07 11:04:48.076 cookbook[767:a0b] <NSThread: 0x8925400>{name = (null), num = 1}

2014-03-07 11:04:48.076 cookbook[767:a0b] -[AppDelegate test6_10] will over

2014-03-07 11:04:48.077 cookbook[767:a0b] <NSThread: 0x8925400>{name = (null), num = 1}

2014-03-07 11:04:49.077 cookbook[767:3707] -[AppDelegate reloadImageView]

2014-03-07 11:04:49.079 cookbook[767:3707] <NSThread: 0x8946a90>{name = (null), num = 2}

2014-03-07 11:04:50.077 cookbook[767:3607] -[AppDelegate reloadScrollView]

2014-03-07 11:04:50.079 cookbook[767:3607] <NSThread: 0x8c3df20>{name = (null), num = 4}

2014-03-07 11:04:51.077 cookbook[767:1403] -[AppDelegate reloadTableView]

2014-03-07 11:04:51.079 cookbook[767:1403] <NSThread: 0x8c6cc60>{name = (null), num = 5}

2014-03-07 11:04:51.080 cookbook[767:1403] __23-[AppDelegate test6_10]_block_invoke34

2014-03-07 11:04:51.081 cookbook[767:1403] <NSThread: 0x8c6cc60>{name = (null), num = 5}




用dispatch_group_async_f怎么写呢?

void reloadAllComponents(void *context){

    AppDelegate *self =

    (__bridge AppDelegate *)context;

    [self reloadTableView];

    [self reloadScrollView];

    [self reloadImageView];

}


-(void)test6_10_2

{

    NSLog(@"%s ", __FUNCTION__);

    NSLog(@"%@",[NSThread currentThread]);

    dispatch_group_t taskGroup = dispatch_group_create();

//    dispatch_queue_t mainQueue = dispatch_get_main_queue();

    dispatch_queue_t mainQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

    dispatch_group_async_f(taskGroup, mainQueue,

                           (__bridge void *)self,

                           reloadAllComponents

                           );

    /* At the end when we are done, dispatch the following block */

    dispatch_group_notify(taskGroup, mainQueue, ^{

        /* Do some processing here */

        NSLog(@"%s ", __FUNCTION__);

        NSLog(@"%@",[NSThread currentThread]);

//        [[[UIAlertView alloc] initWithTitle:@"Finished"

//          message:@"All tasks are finished" delegate:nil

//                          cancelButtonTitle:@"OK" otherButtonTitles:nil, nil] show];

    });

    /* We are done with the group */

//    dispatch_release(taskGroup);

    

    NSLog(@"%s will over", __FUNCTION__);

    NSLog(@"%@",[NSThread currentThread]);

}



打印结果是:

2014-03-07 11:12:27.696 cookbook[845:a0b] -[AppDelegate test6_10_2] 

2014-03-07 11:12:27.697 cookbook[845:a0b] <NSThread: 0x8930460>{name = (null), num = 1}

2014-03-07 11:12:27.697 cookbook[845:a0b] -[AppDelegate test6_10_2] will over

2014-03-07 11:12:27.698 cookbook[845:a0b] <NSThread: 0x8930460>{name = (null), num = 1}

2014-03-07 11:12:30.699 cookbook[845:1403] -[AppDelegate reloadTableView]

2014-03-07 11:12:30.700 cookbook[845:1403] <NSThread: 0x8c275c0>{name = (null), num = 3}

2014-03-07 11:12:32.702 cookbook[845:1403] -[AppDelegate reloadScrollView]

2014-03-07 11:12:32.703 cookbook[845:1403] <NSThread: 0x8c275c0>{name = (null), num = 3}

2014-03-07 11:12:33.705 cookbook[845:1403] -[AppDelegate reloadImageView]

2014-03-07 11:12:33.707 cookbook[845:1403] <NSThread: 0x8c275c0>{name = (null), num = 3}

2014-03-07 11:12:33.708 cookbook[845:1403] __25-[AppDelegate test6_10_2]_block_invoke 

2014-03-07 11:12:33.709 cookbook[845:1403] <NSThread: 0x8c275c0>{name = (null), num = 3}