iOS high concurrent processing corresponding relationship between the amount of data requests and data collection

A processing core of high concurrent requests code is as follows:

// 创建信号量
    dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    // 创建全局并行
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_async(group, queue, ^{
        // 请求
        [self httpRequest];
        dispatch_semaphore_signal(semaphore);
    });
    
    dispatch_group_notify(group, queue, ^{
        // 请求对应信号等待
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
    });

analysis:

First create a parallel queues, create the queue groups, queues and networks need to be addressed requests are added to the group, when the group all finished processing the event queue calls dispatch_group_notify, we need to handle events inside. Because the queue when processing a request to the network "transmitted a request" as the event completion flag (in this case a network request returns data has not been obtained), where it is necessary to control the amount of signal, waits for initiation signal (three times before execution dispatch_group_notify wait signal, each signal respectively corresponding to the notification queue), acquisition request signal the network to return the data in each queue. This will complete the requirements of demand.

If the demand to: the presence of A, B, C three tasks simultaneously, sequentially ABC treatment required when a task is completed and then the next, when the three tasks are completed and then processes the event. In this case only need to read the serial queue can queue (not need to control the amount of signal).

Second, high concurrency processing request after completion of the set of data one relationship

Sequence data set of the requesting time for traversing if concurrent requests transmitted HTTP, since the server response data at different times, will cause a non-correspondence to the request, the following ideas, create a variable dictionary NSMutableDictionarycorresponding to a request to establish a data set relationship. Then traverse the dictionary, according to keythe order of rearranging data collection

        NSMutableDictionary *twoCategoryData = [NSMutableDictionary dictionary];
        [_bigDict setObject:twoSmallDataArr forKey:[NSString stringWithFormat:@"%ld",index]];

        if (_bigDict.count == _oneCategoryData.count) {
            for (int i=0; i<_oneCategoryData.count; i++) {
                NSArray *arr = [_bigDict objectForKey:[NSString stringWithFormat:@"%d",i]];
                [_twoCategoryData addObject:arr];
            }

Reference source project

// 获取一级分类data
- (void)getCategory {
    [HTTPMANGER getFirstLevelCategoryListSuccessedBlock:^(NSDictionary *resultDict) {
        NSLog(@"resultDict:%@",resultDict);
        if (DATAINFO_SUCCESS) {
            // 一级分类数组
            _oneCategoryData = [NSMutableArray array];
            _twoCategoryData = [NSMutableArray array];
            _bigDict = [NSMutableDictionary dictionary];
            for (NSDictionary *dic in DATA) {
                CategoryModel *categoryModel = [CategoryModel initJson:dic];
                [_oneCategoryData addObject:categoryModel];
                
                // 创建信号量
                dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
                // 创建全局并行
                dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
                dispatch_group_t group = dispatch_group_create();
                dispatch_group_async(group, queue, ^{
                    
                    NSUInteger index = [DATA indexOfObject:dic];
                    [self getTwoClassData:dic index:index];

                    dispatch_semaphore_signal(semaphore);
                });
                dispatch_group_notify(group, queue, ^{
                    // 请求对应信号等待
                    dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
                });
            }
        }
    } failedBolck:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
}
// 获取二级分类data
- (void)getTwoClassData:(NSDictionary *)dic index:(NSUInteger)index {
 
    [HTTPMANGER getSecondLevelCategoryListWithCategoryId:dic[@"CATEGORY_ID"] successedBlock:^(NSDictionary *resultDict) {
        NSLog(@"resultDict:%@",resultDict);
        
        NSMutableArray *twoSmallDataArr = [NSMutableArray array];
        NSMutableDictionary *twoCategoryData = [NSMutableDictionary dictionary];
        for (NSDictionary *smalldic in DATA) {
            CategoryModel *smallModel = [CategoryModel initJson:smalldic];
            [twoCategoryData setObject:smallModel.CATEGORY_NAME forKey:@"name"];
            [twoCategoryData setObject:smallModel.CATEGORY_ID forKey:@"type"];
            [twoCategoryData setObject:smallModel.SUPER_CATEGORY_ID forKey:@"super"];
            [twoSmallDataArr addObject:twoCategoryData];
        }
        //[_twoCategoryData addObject:twoSmallDataArr];
        [_bigDict setObject:twoSmallDataArr forKey:[NSString stringWithFormat:@"%ld",index]];
        
        if (_bigDict.count == _oneCategoryData.count) {
            for (int i=0; i<_oneCategoryData.count; i++) {
                NSArray *arr = [_bigDict objectForKey:[NSString stringWithFormat:@"%d",i]];
                [_twoCategoryData addObject:arr];
            }
            [self initSearchBar];
        }
    } failedBolck:^(NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

Guess you like

Origin blog.csdn.net/walkerwqp/article/details/91850505