Q:

AFHTTPSessionManager Delegate issue

here is my issue:
I have a AFHTTPSessionManager file, which is also a singleton, and manage all my API requests to my server. Once I got the answer from the server with a responseObject, I pass it back to the UIViewController who asked for it using a delegate.
My problem is : since my manager is a singleton, if another API request is made in the meantime by another UIViewController, the delegate is set to this controller and when my first request responseObject is received I can't pass it back to the first UIViewController anymore.
I hope it's easy to understand.
What would be the right way to solve this problem ?
Here is what a method looks like in my AFHTTPSessionManager class :
- (void)getStaffForCompany:(int)companyID
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"currentUser"])
    {
        NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
        parameters[@"apiKey"] = agendizeApiKey;
        parameters[@"token"] = [[AGZUserManager sharedAGZUser] currentApplicationUser].token;

        [self GET:[NSString stringWithFormat:@"scheduling/companies/%d/staff", companyID] parameters:parameters success:^(NSURLSessionDataTask *task, id responseObject) {
            if ([self.delegate respondsToSelector:@selector(AGZClient:successedReceiveStaffList:)]) {
                [self.delegate AGZClient:self successedReceiveStaffList:responseObject];
            }
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            if ([self.delegate respondsToSelector:@selector(AGZClient:failedReceiveStaffList:)]) {
                [self.delegate AGZClient:self failedReceiveStaffList:error];
            }
        }];
    }
}

Thanks!

A:

You could define your own completion block and pass your responseObject back to the controller, here is an example CustomCompletion.
Add this to your AFHTTPSessionManager.h just above the @implementation line.
typedef void(^CustomCompletion)(id responseObject);

Update your method to include the CustomCompletion object.
- (void)getStaffForCompany:(int)companyID withCompletion:(CustomCompletion)completion {
    // On success pass the responseObject back like so.
    completion(responseObject);
}

Then where all the magic happens, back in your controller call this method on your singleton and handle the completion.
[SingletonManager getStaffForCompany:1 withCompletion:^(id responseObject) {
    if (responseObject) {
        // do something with this object
    }
}];

I haven't tested this code, but I do something very similar in Swift and it works a treat.

