AFNetworking only runs background tasks when Wi-Fi is available
I would like to use AFNetworking AFHTTPSessionManager
to implement an action to upload data to a web server (like a post action) only when Wi-Fi is available and the action needs to be queued even if cellular is available. I think I need to use AFNetworkReachabilityManager
. But in AFNetworking github it only shows how to work with AFHTTPRequestOperationManager
and manager.operationQueue
.
Hence, I want to ask if I can make a similar stuff using AFHTTPSessionManager
and thisoperationQueue
Below is my thought with some piece of code, does it make sense to do this?
AppDelegate.m
@interface AppDelegate ()
@property (strong, nonatomic) NSURLSession *session;
@property (strong, nonatomic) NSMutableURLRequest *request;
@property (strong, nonatomic) AFHTTPSessionManager *manager;
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self config_sessionManager];
[self config_request];
return YES;
}
- (void)config_sessionManager {
NSURL *baseURL = [NSURL URLWithString:TargetURL];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:kSCIdenitifer];
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:baseURL sessionConfiguration:config];
//actually, what is this operationQueue? I don't quit understand it
//sometimes, I see `mainQueue` was used,
//e.g `[[NSOperationQueue mainQueue] addOperation:op]` in GET example in the Github
NSOperationQueue *operationQueue = self.manager.operationQueue;
[self.manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
switch (status) {
case AFNetworkReachabilityStatusReachableViaWiFi:
[operationQueue setSuspended:NO];
break;
default:
[operationQueue setSuspended:YES];
break;
}
}];
[self.manager.reachabilityManager startMonitoring];
}
- (void)config_request {
self.request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:kTargetURL]];
self.request.HTTPMethod = @"POST";
self.request.HTTPBody = [self.m_str dataUsingEncoding:NSUTF8StringEncoding];
}
- (void)run_task {
NSURLSessionUploadTask * task = [self.manager uploadTaskWithStreamedRequest:self.request progress:nil completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
if (!error) {
//ok, no error
NSLog(@"upload data successfully!!!!");
}
else {
//there is error
NSLog(@"error with uploading data!!!!");
}
}];
[task resume];
}
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
[self run_task];
completionHandler(UIBackgroundFetchResultNewData);
}
@end
So if I do as above, will I put the task config_task
in the Session Manager if Wifi is not available? And if later Wifi is available again, will the task resume and data as I want?
source to share
No one has answered this question yet
Check out similar questions: