iOS 7 - 正确停止停止和'清除'dispatch_queue_t

时间:2015-06-12 17:28:37

标签: ios7 queue suspend

我有一个很长的方法从服务器下载一组数据和文件,我有一个Reachability类来监控网络连接。我想要做的是如果网络断开连接,停止队列并“清除”它。当用户单击按钮重试时,它从头开始。那么,适当的步骤是什么?

是吗

<。>文件中的

@property (nonatomic) dispatch_queue_t background_q;
@property (nonatomic) Reachability *reachMonitor;
<。>文件中的

- (void)downloadBigDataAndFiles {
    self.reachMonitor = [Reachability reachabilityForInternetConnection];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reachabilityValueChanged:) name:kReachabilityChangedNotification object:nil];
    [reachMonitor startNotifier];

    self.background_q = dispatch_queue_create("backgroundMasterQueue", NULL);
    dispatch_async(background_q, ^{
        // Do whatever
    });
}

断开连接时

- (void)reachabilityValueChanged:(NSNotification *)notification {

    if ([self.reachMonitor currentReachabilityStatus] == NotReachable) {
        if (self.background_q) {              // <- is it necessary?
            dispatch_suspend(self.background_q);
            self.background_q = nil;          // <- is it necessary?
        }
    } else if (([self.reachMonitor currentReachabilityStatus] == ReachableViaWWAN) || ([self.reachMonitor currentReachabilityStatus] == ReachableViaWiFi)) {

        // show up retry button...
    }
}

0 个答案:

没有答案