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

/// 创建信号量

    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);


    });

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容