iOS 7 - Proper stop of stop and 'clear' dispatch_queue_t

296 views Asked by At

I am having a long method which download a set of data and files from server and I have a Reachability class to monitor the network connection. What I want to do is if network disconnected, stop the queue and 'clear' it. When user click a button to retry, it start everything from beginning. So, what is the proper steps?

Is it

in .h file

@property (nonatomic) dispatch_queue_t background_q;
@property (nonatomic) Reachability *reachMonitor;

in .m file

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

When disconnected

- (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

There are 0 answers