多个异步网络请求,刷新UI

2018-05-14  本文已影响0人  发呆的日常

/// 创建信号量

    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, ^{

        [[NetAPIManager sharedManager] request_getNewsCountWithCompanyId:self.company.uid andBlock:^(id data, NSError *error) {

            if (!error) {

                newsCount = [data integerValue];

                self.news = [NSString stringWithFormat:@"资讯 %zd", newsCount];

            }

                        dispatch_semaphore_signal(semaphore);

请求成功  发信号

        }];

            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

// 进入等待

    });

        dispatch_group_async(group, queue, ^{

        [[NetAPIManager sharedManager] request_getDrugsCountWithCompanyId:self.company.uid andBlock:^(id data, NSError *error) {

            if (!error) {

                drugsCount = [data integerValue];

                self.self.drugs = [NSString stringWithFormat:@"招商 %zd", drugsCount];

            }

                        dispatch_semaphore_signal(semaphore);

        }];

            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        });

        dispatch_group_async(group, queue, ^{

        [[NetAPIManager sharedManager] request_getTalentsCountWithCompanyId:self.company.uid andBlock:^(id data, NSError *error) {

            if (!error) {

                talentsCount = [data integerValue];

                self.talents = [NSString stringWithFormat:@"资讯 %zd", talentsCount];

            }

            dispatch_semaphore_signal(semaphore);

        }];

        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        });

        dispatch_group_async(group, queue, ^{

        [[NetAPIManager sharedManager] request_getCommentsCountWithCompanyId:self.company.uid andBlock:^(id data, NSError *error) {

            if (!error) {

                commentsCount = [data integerValue];

                self.comments = [NSString stringWithFormat:@"留言 %zd", commentsCount];

            }

            dispatch_semaphore_signal(semaphore);

        }];

            dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);

        });

//    __weak typeof(self) weakSelf = self;

    dispatch_group_notify(group, queue, ^{

主线程刷新UI

        dispatch_async(dispatch_get_main_queue(), ^{

            self.list = @[@"简介", self.news, self.drugs,self.talents ,self.comments];

            [self changeTitle];

        });

            //更新UI操作

            NSLog(@"______+++++%@", self.list);

    });

上一篇下一篇

猜你喜欢

热点阅读