美文网首页
Parser 中GCD的使用(3)

Parser 中GCD的使用(3)

作者: 老猫_2017 | 来源:发表于2020-01-17 10:55 被阅读0次

Parser 之 GCD (3)

PFNetworkActivityIndicatorManager 管理网络指示符

// 单例 dispatch_once
+ (PFNetworkActivityIndicatorManager *)sharedManager {
    static PFNetworkActivityIndicatorManager *manager;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[PFNetworkActivityIndicatorManager alloc] init];
        manager.enabled = YES;
    });
    return manager;
}

    
// 串行队列
_networkActivityAccessQueue = dispatch_queue_create("com.parse.networkActivityIndicatorManager",
                                                        DISPATCH_QUEUE_SERIAL);
// 串行同步数据,异步同步UI
- (void)setNetworkActivityCount:(NSUInteger)networkActivityCount {
    dispatch_sync(_networkActivityAccessQueue, ^{
        self->_networkActivityCount = networkActivityCount;
    });
    dispatch_async(dispatch_get_main_queue(), ^{
        [self _updateNetworkActivityIndicatorVisibilityAfterDelay];
    });
}

- (NSUInteger)networkActivityCount {
    __block NSUInteger count = 0;
    dispatch_sync(_networkActivityAccessQueue, ^{
        count = self->_networkActivityCount;
    });
    return count;
}

- (void)incrementActivityCount {
    dispatch_sync(_networkActivityAccessQueue, ^{
        self->_networkActivityCount++;
    });
    dispatch_async(dispatch_get_main_queue(), ^{
        [self _updateNetworkActivityIndicatorVisibilityAfterDelay];
    });
}

- (void)decrementActivityCount {
    dispatch_sync(_networkActivityAccessQueue, ^{
        self->_networkActivityCount = MAX(self->_networkActivityCount - 1, 0);
    });
    dispatch_async(dispatch_get_main_queue(), ^{
        [self _updateNetworkActivityIndicatorVisibilityAfterDelay];
    });
}

相关文章

网友评论

      本文标题:Parser 中GCD的使用(3)

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