我有一个很长的方法从服务器下载一组数据和文件,我有一个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...
}
}