Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Not Getting Success/Fail Callbacks for Share Extension using AFNetworking

I'm having a bit of a problem with AFNetworking in a share extension. In the didSelectPost, I'm calling:

[[AuthClient sharedClient] POST: @"/v1/events"
    parameters: params success: ^ (AFHTTPRequestOperation * operation, id responseObject) {
        [self.extensionContext completeRequestReturningItems: nil completionHandler: nil];
    }
    failure: ^ (AFHTTPRequestOperation * operation, NSError * error) {
        NSLog(@"error: %@", error);
        [self.extensionContext cancelRequestWithError: error];
    }
];

[AuthClient sharedClient] uses the singleton pattern to get an instance of AFHTTPSessionManager with NSURLSessionConfiguration set with a background identifier.

However, neither the success or failure callbacks are invoked and it just hangs indefinitely until the extension gets killed. Interestingly, the HTTP request finishes fine on the server side; the completion just never gets invoked.

like image 922
iMack Avatar asked Feb 25 '16 06:02

iMack


3 Answers

I guess, the problem isn't with AFNetworking but problem is with that singleton class. You have not specified your app's architecture, but I'm assuming that, you may calling multiple APIs in different view controllers using the same [AuthClient sharedClient] singleton at the same time. Hence, you're actually receiving a success call but not for the above call but some where else in your controller. I have faced the exact issue in my UITabbarController based application where I was making API calls in viewDidLoad of different view controllers and my tester was continuously changing all tabs. Thus, I tried to get response for first view controller (1st tab) and used to get response in second view controller (2nd tab), which was wrong!

like image 140
Hemang Avatar answered Nov 19 '22 04:11

Hemang


1) Try to execute this request in Postman, for check the right way and parameters, if ok -> 2.

2) Try to use session configuration like this

NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];

// Initialize Session Manager
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:sessionConfiguration];

at this point we can assume that your singletone was initialised for another request with different configuration.

3) similar problem

4) about AFNetworking background

P.S. Try to use simple asynchronous on main thread, with spinner while waiting response

like image 1
Mr.Fingers Avatar answered Nov 19 '22 05:11

Mr.Fingers


Some things I'd try:

  • Do a sanity check with a network trace; and ensure that the data you expect is reaching the client.
  • Set a breakpoint and look for any blocked threads or anomalies (even consider breaking on the Apple networking APIs themselves ie. CFNetworking or NSURLSession etc.).
  • Check that the lambda/completion blocks are being properly set/retained by your singleton object / try to write a simple test routine without using the singleton to see if it makes a difference (such as by manually instantiating the class and using it).
  • It's not clear from the snippet if the 'success' completion handler prints out a debug message - consider adding an NSLog there too, if one's not already in the handler.

Hopefully some of this can bring you closer to an idea of what's causing the problem.

like image 1
kvr Avatar answered Nov 19 '22 06:11

kvr