iphone - iOS instance variables not initialised from within a block -


i using parse.com backend app. need information backend , init instance information. use code in order so:

    - (id) initwithteamid:(nsstring *)teamid { __block nsstring *str; __block frfteam *blockself = self; pfquery *query = [pfquery querywithclassname:@"teams"]; [query getobjectinbackgroundwithid:teamid block:^(pfobject *object, nserror *error) {     str = [object objectforkey:@"teamname"];     (void)[blockself initwithname:str players:nil thumb:nil]; }];  return self; } 

when code done self.name set null, doing wrong? thank you!

try code:

// call init on object, setup team id - (id)initwithteamid:(nsstring *)teamid {     self = [super init];     if (self) {         [self setupwithteamid:teamid];     }      return self; }  - (void) setupwithteamid:(nsstring *)teamid {     __weak frfteam *blockself = self;      pfquery *query = [pfquery querywithclassname:@"teams"];     [query getobjectinbackgroundwithid:teamid block:^(pfobject *object, nserror *error) {         nsstring *name = [object objectforkey:@"teamname"];         nslog(@"received name: %@ object: %@", name, object);         [blockself setname:name];     }]; } 

then, change name of method initwithname:... because isn't init method because have done init before calling setupwithteamid:.

if need parse bit done before init method returns, should:

  1. call parse details before calling init on object
  2. use getobjectwithid: --- not recommended blocks thread in init, bad idea

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -