美文网首页
多个异步网络请求,刷新UI

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

作者: 发呆的日常 | 来源:发表于2018-05-14 17:36 被阅读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);

        });

    相关文章

      网友评论

          本文标题:多个异步网络请求,刷新UI

          本文链接:https://www.haomeiwen.com/subject/dyjjdftx.html