Search code examples
objective-cafnetworkingnsurlsessionreachability

Resume all upload tasks when network re-connect in AFNetworking


I´m using AFNetworking 3.0. I have successfully uploaded tasks using background session. Now I want to suspend and resume all tasks based on network reachablity. Like suspend tasks when no-network and resume task when network reconnect.

Code:

static AFURLSessionManager *manager;

static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{

    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:@"wts.Demo-Upload"];
    sessionConfiguration.HTTPMaximumConnectionsPerHost = 20;
    manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];
});

[manager setDidFinishEventsForBackgroundURLSessionBlock:^(NSURLSession * _Nonnull session) {
    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appDelegate.backgroundSessionCompletionHandler) {
        void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
        appDelegate.backgroundSessionCompletionHandler = nil;
        completionHandler();
    }

    NSLog(@"All tasks are finished");
}];



NSOperationQueue *operationQueue = manager.operationQueue;

[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {

    switch (status) {
        case AFNetworkReachabilityStatusNotReachable:
            // we need to notify a delegete when internet conexion is lost.
            // [delegate internetConexionLost];
            NSLog(@"No Internet Conexion");
            break;
        case AFNetworkReachabilityStatusReachableViaWiFi:
            NSLog(@"WIFI");
            break;
        case AFNetworkReachabilityStatusReachableViaWWAN:
            NSLog(@"3G");
            break;
        default:
            NSLog(@"Unkown network status");
            [operationQueue setSuspended:YES];
            break;
    }
}];
    [manager.reachabilityManager startMonitoring];

Solution

  • Finally found solution. First get all upload tasks and resume it.

    [manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"No Internet Connection");
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WiFi");
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"3G");
                for (NSURLSessionUploadTask *uploadTask in [manager uploadTasks]) {
                    [uploadTask resume];
                }
                break;
            default:
                NSLog(@"Unknown network status");
                [operationQueue setSuspended:YES];
                break;
        }
    }];
    

    Above code works fine when app is on foreground. But setReachabilityStatusChangeBlock is never called when app is in the background. Can anyone know how to call reachability block even app is in background?