在 AFNetworking 中重新连接网络时恢复所有上传任务
Resume all upload tasks when network re-connect in AFNetworking
我正在使用 AFNetworking 3.0
。我已经使用后台会话成功上传了任务。现在我想暂停和恢复基于网络可达性的所有任务。喜欢在没有网络时暂停任务并在网络重新连接时恢复任务。
代码:
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];
终于找到解决办法了。
先搞定所有上传任务再恢复。
[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;
}
}];
以上代码在应用程序处于前台时运行良好。但是当应用程序在后台时, setReachabilityStatusChangeBlock
永远不会被调用。
谁知道即使应用程序在后台如何调用可达性块?
我正在使用 AFNetworking 3.0
。我已经使用后台会话成功上传了任务。现在我想暂停和恢复基于网络可达性的所有任务。喜欢在没有网络时暂停任务并在网络重新连接时恢复任务。
代码:
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];
终于找到解决办法了。 先搞定所有上传任务再恢复。
[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;
}
}];
以上代码在应用程序处于前台时运行良好。但是当应用程序在后台时, setReachabilityStatusChangeBlock
永远不会被调用。
谁知道即使应用程序在后台如何调用可达性块?