ios - passing __block parameters to class method (for get request) -


i want create following class method:

+(void) getvalue4key:(nsstring*)p_key andset:(id)p_variable {         nsstring *baseurlstring = <<myurl>>;     @try{         afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager];         [manager get:baseurlstring           parameters:nil              success:^(afhttprequestoperation *operation, id responseobject) {                  nsdictionary* element = responseobject[0];                  element = [element objectforkey:@"fields"];                   p_variable = [element objectforkey:@"value"];              }              failure:^(afhttprequestoperation *operation, nserror *error) {                  nslog(@"getsystemvariablekey error: %@",error);              }];     }     @catch (nsexception *exception) {         nslog(@"exception %@", exception);     } } 

two questions:

  1. i following error: variable not assignable (missing __block type specifier) how can set block method parameter?
  2. how call function should pass variable &self.setme?

i don't think approach of passing ivar reference set asynchronously @ later time approach. if object (referred in question #2 self in self.setme) destroyed before request completes? you're going have random crashes.

instead, should approach completion block callers can use set ivar:

+ (void)getvalue4key:(nsstring*)p_key        withcompletion:(void (^)(id value))completion {         nsstring *baseurlstring = <<myurl>>;     @try{         afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager];         [manager get:baseurlstring           parameters:nil              success:^(afhttprequestoperation *operation, id responseobject) {                  nsdictionary* element = responseobject[0];                  element = [element objectforkey:@"fields"];                   id value = [element objectforkey:@"value"];                  if (completion) {                      completion(value);                  }              }              failure:^(afhttprequestoperation *operation, nserror *error) {                  nslog(@"getsystemvariablekey error: %@",error);                  if (completion) {                      completion(nil);                  }              }];     }     @catch (nsexception *exception) {         nslog(@"exception %@", exception);     } } 

then call function this:

yourobject *__weak weakself = self; [yourobject getvalue4key:@"your_key" completion:^(id value){     weakself.setme = value; }]; 

now, if self gets destroyed, weakself become nil callback be, basically, no-op.

this has added advantage of not needing pass ivar pointers reference, note, doesn't happen @ in ios frameworks (nserror being exception can think of off-hand).


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -