使用setReachabilityStatusChangeBlock使设备脱机时的AFNetworking 2.0队列请求不执行任何操作 [英] AFNetworking 2.0 queue request when device is offline with setReachabilityStatusChangeBlock does nothing

查看:456
本文介绍了使用setReachabilityStatusChangeBlock使设备脱机时的AFNetworking 2.0队列请求不执行任何操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试提出一种解决方案,以在设备离线时使用AFNetworking将HTTP请求排队,因此,当设备恢复在线时,请求就可以完成。据我所知,可以设置 setReachabilityStatusChangeBlock:参数。

I've been trying to come up with a solution to queue HTTP requests using AFNetworking when the device is offline, so when it goes back online the requests gets done. As far as I've been able to understand, this is possible setting the setReachabilityStatusChangeBlock: parameter.

到目前为止这就是我所拥有的:

So far this is what I have:

// ViewController.h
@interface XYZTicketViewController : UIViewController<NSURLConnectionDelegate> // This is from before I started using AFNetworking, I'm intending to change all the requests to use AFNetworking in the near future.   
@end 


// ViewController.m
(...)
#import <AFHTTPRequestOperationManager.h>
#import <AFNetworkReachabilityManager.h>
(...)
@interface XYZTicketViewController ()
- (void)viewDidLoad
(...)
{
NSURL *baseURL = [NSURL URLWithString:@"http://54.213.167.202"];
AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:baseURL];

NSOperationQueue *operationQueue = manager.operationQueue;
[manager.reachabilityManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            [operationQueue setSuspended:NO];
            NSLog(@"WIFI");
            break;
        case AFNetworkReachabilityStatusNotReachable:
        default:
            [operationQueue setSuspended:YES];
            NSLog(@"oflline, baby");
            break;
    }
}];

NSDictionary *parameters = @{@"action": @"login", @"user": @"mail.address@gmail.com", @"pass": @"howdoyouturnthison"};
[manager GET:@"http://54.213.167.202/api.php"  parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error);
}];
(...)
}

我找不到任何示例,但我阅读 这是有可能的,但是到目前为止,当在线状态发生变化时,任何事情都会发生。

I cannot find any example but I read here that it is possible, but so far anything happens when the online status changes.

希望您能帮助我

推荐答案

您需要先调用startMonitoring,然后再调用setReachabilityStatusChangeBlock

You need to call startMonitoring, before you call setReachabilityStatusChangeBlock

[manager.reachabilityManager startMonitoring ];

如果您使用的是AFNetworking 2.0,我建议您执行以下操作:

If you are using AFNetworking 2.0, I suggest following:

[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
    DLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
    switch (status) {
        case AFNetworkReachabilityStatusReachableViaWWAN:
        case AFNetworkReachabilityStatusReachableViaWiFi:
            [operationQueue setSuspended:NO];
            NSLog(@"WIFI");
            break;
        case AFNetworkReachabilityStatusNotReachable:
        default:
            [operationQueue setSuspended:YES];
            NSLog(@"offline, baby");
            break;
    }
}];

这篇关于使用setReachabilityStatusChangeBlock使设备脱机时的AFNetworking 2.0队列请求不执行任何操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆