美文网首页
iOS开发GCD信号量

iOS开发GCD信号量

作者: lczalh | 来源:发表于2018-11-29 10:30 被阅读12次
//创建信号量
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_async(queue, ^{
         // 等待信号量
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        [BadgeCountModel HomeBadgeCountBlock:^(NSDictionary *data) {
            NSNumber *num = data[@"num"];
            if (num.integerValue > 0 ) {
                weakSelf.rightBt.isShowBadge = YES;
            } else {
                weakSelf.rightBt.isShowBadge = NO;
                
            }
            // 发送信号量
            dispatch_semaphore_signal(semaphore);
        } andError:^(NSError *error) {
            dispatch_semaphore_signal(semaphore);
        }]; 
    });
    
    dispatch_async(queue, ^{
        dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
        
        [BadgeCountModel HomeDetailBadgeCountBlock:^(NSArray *data) {
            if (!weakSelf.badgeDict) {
                weakSelf.badgeDict = [NSMutableDictionary dictionary];
            }
            [weakSelf.badgeDict removeAllObjects];
            NSMutableArray *badgeArray = [NSMutableArray array];
            for (NSDictionary *dict in data) {
                BadgeCountModel *model = [[BadgeCountModel alloc] init];
                model.code = [NSString stringWithFormat:@"%@",dict[@"key"]];
                model.value = [NSString stringWithFormat:@"%@",dict[@"value"]];
                [badgeArray addObject:model];
                NSIndexPath *path = [self getIndexPathWithCode:model.code];
                if (!path) {
                    continue;
                }
                [weakSelf.badgeDict setObject:model.value forKey:path];
            }
            [weakSelf.collectionView reloadData];
            [[GLMenuManager shareInstance] setRedPointArray:badgeArray];
            dispatch_semaphore_signal(semaphore);
        } andError:^(NSError *error) {
            dispatch_semaphore_signal(semaphore);
        }];

相关文章

网友评论

      本文标题:iOS开发GCD信号量

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