Parser 中GCD的使用(3)

2020-01-17  本文已影响0人  老猫_2017

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

上一篇下一篇

猜你喜欢

热点阅读