Queue group

  • There is such a need
  • First: Perform two time-consuming operations asynchronously
  • Second: wait for both asynchronous operations to complete, then return to the main thread to execute the operation
  • If you want to efficiently implement this requirement, consider using queue groups
- (void)group
{
    // Create a global concurrent queue
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    Create a queue group
    dispatch_group_t group = dispatch_group_create();
    // 1
    NSArray __block *uArray = nil; // The first data request is placed in this array

    dispatch_group_async(group, queue, ^{
        dispatch_group_enter(group); // This sign is very important
        @weakify(self)
        Rac network requests can be replaced with their own network requests
        [[self.otherViewModel.giftListCommand execute:@{@"type": @ (0), @"targetUid":SAFE_STR(self.userId)}] subscribeNext:^(id  _Nullable x) {
            // The array succeeds
            @strongify(self)

            LPGiftListModel * listModel = x[0];

            NSArray *tempArray = [NSArray arrayWithArray:listModel.list];
            // Intercepts the first 16 bytes of array data
            uArray = [tempArray subarrayWithRange:NSMakeRange(0.16)];

            NSLog(11111% @ "@",uArray);
            // Leave the tag
            dispatch_group_leave(group);
        }];
    });
    // 2
    NSArray __block *demoArray = nil;

    dispatch_group_async(group, queue, ^{

        dispatch_group_enter(group);
        @weakify(self)
        [[self.otherViewModel.giftListCommand execute:@{@"type": @ (2), @"targetUid":SAFE_STR(self.userId)}] subscribeNext:^(id  _Nullable x) {
            LPGiftListModel * listModel = x[0];
            NSArray *tempArray = [NSArray arrayWithArray:listModel.list];
            demoArray = [tempArray subarrayWithRange:NSMakeRange(0.8)];
            NSLog(22222% @ "@",demoArray);
            dispatch_group_leave(group);
        }];
    });

    // 3. Merge two arrays of data retrieves into one array
    dispatch_group_notify(group, queue, ^{
        // Go back to the main thread to display the image
        dispatch_async(dispatch_get_main_queue(), ^{
            // 4. Display the new image
            NSMutableArray *array = [NSMutableArray arrayWithArray:uArray];
            [array addObjectsFromArray:demoArray];
            self.giftDataArr = array;
            NSLog(33333% @ "@",array);
        });
    });
}
Copy the code
  • Notice that I’m ignoring error at the top and I’m going to have to leave the group if there’s an error.
  • Each task requires a pair of Enter and leave

  • This is a previously written data request, not an asynchronous request, so it doesn’t need to be identified
- (void)group
{
    // Create a global concurrent queue
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    Create a queue group
    dispatch_group_t group = dispatch_group_create();
    
    // 1. Download picture 1
    dispatch_group_async(group, queue, ^{
        // The network path of the image
        NSURL *url = [NSURL URLWithString:@"http://img.pconline.com.cn/images/photoblog/9/9/8/1/9981681/200910/11/1255259355826.jpg"];
        
        // Load the image
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        // Generate the image
        self.image1 = [UIImage imageWithData:data];
    });
    
    // 2
    dispatch_group_async(group, queue, ^{
        // The network path of the image
        NSURL *url = [NSURL URLWithString:@"http://pic38.nipic.com/20140228/5571398_215900721128_2.jpg"];
        
        // Load the image
        NSData *data = [NSData dataWithContentsOfURL:url];
        
        // Generate the image
        self.image2 = [UIImage imageWithData:data];
    });
    
    // 3. Combine pictures 1 and 2 into a new picture
    dispatch_group_notify(group, queue, ^{
        // Open a new graphics context
        UIGraphicsBeginImageContext(CGSizeMake(100.100));
        
        // Draw a picture
        [self.image1 drawInRect:CGRectMake(0.0.50.100)];
        [self.image2 drawInRect:CGRectMake(50.0.50.100)];
        
        // Get the picture in context
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext(a);// End the context
        UIGraphicsEndImageContext(a);// Go back to the main thread to display the image
        dispatch_async(dispatch_get_main_queue(), ^{
            // 4. Display the new image
            self.imageView.image = image;
        });
    });
}
Copy the code