记录一次CGD队列组的使用

  问题引入: 接到一个这样的需求,属于订餐App, 用户在前一个页面A选择了主菜,确认之后进入下一个页面B, 在B页面用户可以继续为之前选择的主菜选择配菜,并且也可以选择多份配菜,比如主菜是白菜炒肉,配菜可以是凉拌海带。按照合理的逻辑应该是,在A页面请求主菜的数据时,接口应该统一返回某一个主菜对应的配菜数据。然而,现实情况是,返回主菜的接口只是单单返回了主菜数据,配菜数据需要根据主菜标识再次请求接口。先不去吐槽这不合理的逻辑了,问了安卓的同事,说他也是迫于无奈,也是在B页面再次请求配菜数据的,毕竟,后台的接口就是这样写的,安卓都搞完了,就一个iOS开发还能怎么样呢,开做吧。
    思考之后,觉得可以用GCD队列组解决上述需求。下面贴上相关代码。

pragma mark 网络请求(请求用户所选主菜的配菜数据)&&UI界面逻辑处理

  • (void)requestTheSideDishDataForTheSelectedMainCourse{
    // 注意:self.aryData——>用户所选择的主菜
    NSMutableArray *productIds = [NSMutableArray arrayWithCapacity:0];
    NSMutableArray *productNames = [NSMutableArray arrayWithCapacity:0];
    for (generalizeProductModel *model in self.aryData) {
    NSString *proID = [NSString stringWithFormat:@"%@",model.product_id];
    // 业务所需
    if (proID.length > 0) {
    [productIds addObject:model.product_id];
    }
    }
    // 创建队列
    dispatch_group_t downloadGroup = dispatch_group_create();
    // 所有配菜数据
    NSMutableArray *dishesTotalArr = [NSMutableArray arrayWithCapacity:0];
    for (NSUInteger i = 0; i < productIds.count; i++) {
    // 封装参数
    NSDictionary *dicUser = kUserObjectForKey(@"loginMessage");
    NSMutableDictionary *dic = [@{@"usersId": dicUser[@"usersId"],
    @"socketCode": dicUser[@"socketCode"],
    @"productId": [productIds objectAtIndex:i],
    } mutableCopy];
    NSNumber *isCustom = [dicUser objectForKey:@"isCustom"];
    if ([isCustom isEqual:@(YES)]) {
    [dic setObject:@(1) forKey:@"buy"];
    }
    NSString *url = @"Expend/proPartsList";
    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
    // 追加任务
    dispatch_group_enter(downloadGroup);
    // 2.发送网络请求
    [INV commonCommit:[dic mutableCopy] string:url success:^(id success) {
    NSLog(@"success:%@", success);
    if ([success[@"status"] isEqual:@(1)]) {
    NSArray *datas = (NSArray *)success[@"data"];
    // 表明主菜有配菜
    if (datas.count > 0) {
    for (NSDictionary *dishesDic in datas) {
    PartProductModel *model = [PartProductModel initWithData:dishesDic];
    model.leftUpCount = 0;
    [array addObject:model];
    }
    // 取出与之对应的主菜模型
    generalizeProductModel *model = [self.aryData objectAtIndex:i];
    if (model.product_name.length > 0) {
    [productNames addObject:model.product_name];
    }
    }
    [dishesTotalArr addObject:array];
    }else{
    [LCProgressHUD showMessage:success[@"msg"]];
    }
    dispatch_group_leave(downloadGroup);
    } failure:^(id failure) {
    NSLog(@"failure:%@", [failure description]);
    }];
    }
    // 通知队列,所有任务都已处理(所有的请求都已处理完毕)
    dispatch_group_notify(downloadGroup, dispatch_get_main_queue(), ^{
    NSLog(@"dishesTotalArr:%@, count:%ld", dishesTotalArr, dishesTotalArr.count);
    NSLog(@"all request end");
    // 找到关联,赋值
    self.totalDishesArray = dishesTotalArr;
    // 刷新UI界面
    if (self.isMultipleData && self.tableView.tag == 100){
    // 布局底部视图
    // 215 * i + 220 + (30 + 120)
    HLShopCarCustomTableFooterView *tableFooterView = [[HLShopCarCustomTableFooterView alloc] init];
    tableFooterView.backgroundColor = [UIColor colorWithRed:0.922 green:0.922 blue:0.961 alpha:1.000];
    // 所有配菜数据赋值(数组嵌套数组,数组里面装模型)(基于aryData请求的配菜数据)
    tableFooterView.sideDishesTotalArr = self.totalDishesArray;
    CGFloat spaceHeight = (appW - 30) / 3.0 + 85 + 30;
    if (tableFooterView.companyTasteArr.count > 0) {
    tableFooterView.frame = CGRectMake(0, 0, appW, spaceHeight * self.totalDishesArray.count + 155 + 220);
    }else{
    tableFooterView.frame = CGRectMake(0, 0, appW, spaceHeight * self.totalDishesArray.count + 220);
    }
    // 用户选中的所有主菜名称
    tableFooterView.productNameArr = productNames;
    // 用户选中的所有主菜模型数组(数组装模型)
    tableFooterView.productArray = self.aryData;
    // 设置代理
    tableFooterView.delegate = self;
    tableFooterView.bottomView.totalL.text = self.subTotalPriceStr;
    self.footerView = tableFooterView;
    self.tableView.tableFooterView = tableFooterView;
    [self.tableView reloadData];
    }
    });
    }
    上面用到了创建队列组,进入队列组,退出队列组,以及通知队列组的相关GCD函数。

你可能感兴趣的:(记录一次CGD队列组的使用)