3

I use AFHTTPSessionManager for sending requests to a server, and use Reachability logic to check if there is a connection.

The code for the request is simple:

   [manager POST:urlString parameters:parameters  success:^(NSURLSessionDataTask *task, id responseObject) {
       //Parse data...
   } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"Error: %@", error);
   }];

Also, there is code that checks availability in manner like example in GitHub:

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

When status changes (AFNetworkReachabilityStatusNotReachable to AFNetworkReachabilityStatusReachableViaWiFi for example), and request is resumed, why failure block is executed? What is the reason for that? Am I doing something wrong?

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
Foriger
  • 459
  • 5
  • 30
  • Have you started the AFNetworkReachabilityManager monitoring in the AppDelagate? – xicocaio Jan 27 '14 at 22:37
  • No.It is started , when manager is initialised. Is there a difference,where reachability manager manager is started? – Foriger Jan 28 '14 at 07:44
  • What happens when you comment the `setReachabilityStatusChangeBlock`? – xicocaio Jan 28 '14 at 16:45
  • Request executes, but since queue is not suspended fails with `NSURLErrorCannotConnectToHost` code. – Foriger Jan 28 '14 at 16:51
  • If you are using a baseUrl different than the default for initializing `AFHTTPSessionManager`, what happens when you change it to something like 'http://google.com'? – xicocaio Jan 28 '14 at 16:58
  • Have you tried my edit2 suggestion? – xicocaio Jan 29 '14 at 16:38
  • Look below at @Vytis answer - it is the only one that explains what is really going on here. The reason you are seeing these failures when connectivity is restored is because suspending the queue _does not_ prevent the operation from being attempted, rather, it only prevents the callback from being called. So, when you resume the queue, you start to get the callbacks from those failed operations that happened while you were offline. – Danny Feb 20 '15 at 03:26

4 Answers4

1

operationQueue in AFURLSessionManager is only used for delegate callbacks (see documentation in the header).

I suspect the request is carried out when offline, but the callbacks are suspended and fire only when operationQueue is resumed.

However I haven't yet found a way to resume requests after coming online.

Vytis
  • 1,170
  • 8
  • 13
  • Oh, I forgot this question:) I found a solution but it is specific case that maybe works only for me.It works as follows: If request fail because of Internet connection I add it his specific properties (Like type, params,etc) to my core data model.When network is reachable again, with using "AFNetworkingReachabilityDidChangeNotification" requests is re-executed with this parameters. HTH – Foriger Aug 05 '14 at 15:03
0

I think that it can be a network error.

Infact: AFNetworking doesn't check Internet for you, so if it returns a failure, there's a connectivity error or another logic error (I suppose that isn't the second case).

First of all: your reachability manager what test? What ip address or domain is try to reaching to? Because if you have a reachability status that returns you a wifi connection, it don't tells you that your Web service responds to you.

If you test your internet connection when you are into a AFNetworking failure block it's important to test the specific address of your associated Web server. You can also make more than one try as soon as the Internet connection changed because you can have wifi network but not yet Internet reachability (while dynamic ip address is assigning, for example).

ricky.tribbia
  • 124
  • 10
  • First, thank you for answer. I have a baseURL property like this one: `NSURL *rootURL = [NSURL URLWithString:@"http://example.com/"];`, and `AFHTTPSessionManager` is initialised with this root URL.According to documentation AFNetworking reachability manager observes for changes in this host. So if status changes (host becames active or WIFI is on ), operation queue have to be resumed, and request have to be executed again. – Foriger Jan 27 '14 at 19:36
0

When using the AFNetworkReachabilityManager, i usually start it in the AppDelegate.m. I don't actually remember if it has to be started there, but i think it should. If you are not starting it in there, try it, like this:

- (void)applicationDidBecomeActive:(UIApplication *)application {

   [[AFNetworkReachabilityManager sharedManager] startMonitoring];

   // Rest of your code here
}

Edit:

Also try:

NSOperationQueue * operationQueue = [[NSOperationQueue alloc] init];
[operationQueue addOperation:manager.operationQueue];
[manager.operationQueue release]; // as i have read you must do this release, but not sure
// rest of your code here

You have probably seen this post but, if not, maybe it helps: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues

Edit 2:

Supposing you are trying to simulate the situation where you have no internet connection from the beginning of the enqueue, maybe your problem is similar to this AFNetworking 2.0 Reachability .

In this sense you should probably set a check for reachability before the enqueue operation, like this:

if ([AFNetworkReachabilityManager sharedManager].reachable) {
   NSOperationQueue *operationQueue = manager.operationQueue;
}

Let me know if it works.

Community
  • 1
  • 1
xicocaio
  • 867
  • 1
  • 10
  • 27
  • Thanks for suggestion.I've tried this.Queue suspending, but when restore it (`[operationQueue setSuspended:NO];`) every time failure block is executed.That means, that for some reason host is still unreachable. I'm missing something probably, but what? – Foriger Jan 28 '14 at 15:29
  • You cannot use release in Xcode5,but tried approach with other operaion queue, not works. – Foriger Jan 28 '14 at 16:14
  • Yeah, normally you don't use release when using ARC. But in this post that i passed the link, it states that you should, besides using ARC. – xicocaio Jan 28 '14 at 16:41
0

Does this happen on device, or simulator only? Because I had issues with Reachability on the Xcode 5 simulator, but it's working fine on device (My problem was that when it has to change from Offline to Online mode it says it's still Offline and throws some error)