iOS 多组数据队列上传

2018-09-04  本文已影响26人  it彭于晏

尽量把所要调用的函数写成block形式的

示例如下,仅供参考:

//2.创建上传队列——————先取全部数据-----队列单笔上传操作---轮询分批复查————————count<0结束
-(void)createTackAllPsot{
    [self getAllTackDic];
    if (psotArr.count<1) {
        return;
    }
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
    Context.isPosting = YES;
     dispatch_semaphore_t sema = dispatch_semaphore_create(0);
     dispatch_group_t group = dispatch_group_create();
        for (NSDictionary *postDic in psotArr){//创建上传队列
            [[SDKManager shareManager]initTimViewQueryData:@[postDic[@"bizGuid"]] WithResult:^(BOOL result, id resultString) {
                if (result == NO) {//检查是否有 ke上传的影像
                    dispatch_group_enter(group);
                    dispatch_async(dispatch_get_global_queue(0, 0), ^{
                        dispatch_semaphore_t sema2 = dispatch_semaphore_create(0);
                        //数据交易 上传
                        [self yxkjPostTask:postDic withResultData:^(BOOL result, NSString *resultString) {
                            dispatch_semaphore_signal(sema2);
                        }];
                        dispatch_semaphore_wait(sema2, DISPATCH_TIME_FOREVER);
                        dispatch_group_leave(group);
                    });
                }else{
                    dispatch_group_enter(group);
                    dispatch_async(dispatch_get_global_queue(0, 0), ^{
                        dispatch_semaphore_t sema3 = dispatch_semaphore_create(0);
                        [[SDKManager shareManager]uploadImagesData:postDic WithResult:^(BOOL result, id resultString) {
                            if (result) {
                                NSMutableDictionary *newDic = [NSMutableDictionary dictionary];
                                [newDic addEntriesFromDictionary:postDic];
                                [newDic addEntriesFromDictionary:resultString];
                                [self yxkjPostTask:newDic withResultData:^(BOOL result, NSString *resultString) {
                                    dispatch_semaphore_signal(sema3);
                                }];
                            }else{
                                if ([resultString isEqualToString:@"yxkjError"]) {//yxkj上传失败
                                    [self updataTackErrorWithTackId:postDic[@"TaskId"] withErrorString:@"030" withResultData:^(BOOL result, NSString *resultString) {
                                        dispatch_semaphore_signal(sema3);
                                    }];
                                }
                            }
                        }];
                        dispatch_semaphore_wait(sema3, DISPATCH_TIME_FOREVER);
                        dispatch_group_leave(group);
                    });
                }
            }];
            dispatch_group_notify(group, dispatch_get_main_queue(), ^{
                dispatch_semaphore_signal(sema);
            });
            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
        }
        Context.isPosting = NO;
        [self createTackAllPsot];
    });
   


}

上一篇下一篇

猜你喜欢

热点阅读