ios - AFHTTPSessionManager Delegate issue -
here issue: have afhttpsessionmanager file, singleton, , manage api requests server. once got answer server responseobject, pass uiviewcontroller asked using delegate.
my problem : since manager singleton, if api request made in meantime uiviewcontroller, delegate set controller , when first request responseobject received can't pass first uiviewcontroller anymore.
i hope it's easy understand.
what right way solve problem ?
here method looks in 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!
you define own completion block , pass responseobject controller, here example customcompletion.
add afhttpsessionmanager.h above @implementation line.
typedef void(^customcompletion)(id responseobject); update method include customcompletion object.
- (void)getstaffforcompany:(int)companyid withcompletion:(customcompletion)completion { // on success pass responseobject so. completion(responseobject); } then where magic happens, in controller call method on singleton , handle completion.
[singletonmanager getstaffforcompany:1 withcompletion:^(id responseobject) { if (responseobject) { // object } }]; i haven't tested code, similar in swift , works treat.
Comments
Post a Comment