【发布时间】:2015-03-21 20:09:06
【问题描述】:
目前我正在使用AFHTTPRequestOperationManager 对一个简单的离线请求进行排队,但它似乎无法以预期的方式工作:
这是负责的代码,下面是不同的执行模式:
@interface ViewController ()
{
AFHTTPRequestOperationManager *manager;
}
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
manager = [AFHTTPRequestOperationManager manager];
NSOperationQueue *operationQueue = manager.operationQueue;
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status){
NSLog(@"Reachability: %@", AFStringFromNetworkReachabilityStatus(status));
switch (status) {
case AFNetworkReachabilityStatusReachableViaWWAN:
case AFNetworkReachabilityStatusReachableViaWiFi:
NSLog(@"Operation: %@", operationQueue.operations);
[operationQueue setSuspended:NO];
NSLog(@"ONLINE");
break;
case AFNetworkReachabilityStatusNotReachable:
default:
NSLog(@"Operation: %@", operationQueue.operations);
[operationQueue setSuspended:YES];
NSLog(@"OFFLINE");
break;
}
}];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
[manager GET:@"http://www.google.com"
parameters:nil
success:^(AFHTTPRequestOperation *operation, id response){
NSLog(@"success");
}
failure:^(AFHTTPRequestOperation *operation, NSError *failure){
NSLog(@"failure");
}];
}
模式 1:
- AirPlane 模式下的设备
- 运行
控制台输出:
2015-03-21 16:03:54.486 OfflineSupport[928:227748] Reachability: Not Reachable
2015-03-21 16:03:54.494 OfflineSupport[928:227748] Operation: (
"<AFHTTPRequestOperation: 0x1701d0c20, state: isExecuting, cancelled: NO request: <NSMutableURLRequest: 0x170014ab0> { URL: http://www.google.com }, response: (null)>"
)
2015-03-21 16:03:54.494 OfflineSupport[928:227748] OFFLINE
2015-03-21 16:03:54.544 OfflineSupport[928:227748] failure
- Wifi 已激活
控制台输出续:
2015-03-21 16:04:05.594 OfflineSupport[928:227748] Reachability: Reachable via WiFi
2015-03-21 16:04:05.595 OfflineSupport[928:227748] Operation: (
)
2015-03-21 16:04:05.595 OfflineSupport[928:227748] ONLINE
模式 2:
- Wifi 活跃
- 运行
控制台输出:
2015-03-21 16:05:43.818 OfflineSupport[934:228478] Reachability: Reachable via WiFi
2015-03-21 16:05:43.826 OfflineSupport[934:228478] Operation: (
"<AFHTTPRequestOperation: 0x1701dde20, state: isExecuting, cancelled: NO request: <NSMutableURLRequest: 0x17001ad10> { URL: http://www.google.com }, response: (null)>"
)
2015-03-21 16:05:43.826 OfflineSupport[934:228478] ONLINE
2015-03-21 16:05:43.960 OfflineSupport[934:228478] success
- AirPlane 已激活
控制台输出续:
2015-03-21 16:05:53.437 OfflineSupport[934:228478] Reachability: Not Reachable
2015-03-21 16:05:53.438 OfflineSupport[934:228478] Operation: (
)
2015-03-21 16:05:53.438 OfflineSupport[934:228478] OFFLINE
在模式 1 中,请求导致失败块,因为没有访问权限。但是当设备上线时,请求不再执行。我在这里缺少什么吗?我必须在操作队列或失败块中配置一些东西吗?
参考:AFNetworking 2.0 queue request when device is offline with setReachabilityStatusChangeBlock does nothing,IOS - best way to queue requests to be sent when connection is reestablished
【问题讨论】:
标签: ios httprequest afnetworking