iphone - IOS access a variable outside a block -
this question has answer here:
i have following:
__block alassetsgroup* album = nil; alassetslibrary* library = [alassetslibrary new]; [library enumerategroupswithtypes: alassetsgroupalbum usingblock: ^ (alassetsgroup *group, bool *stop) { if (group) { nsstring* title = [group valueforproperty: alassetsgrouppropertyname]; if ([title isequaltostring: @"test1"]) { album = group; *stop = yes; nslog(@"sd%@", album.description); } } else { // afterwards self.images = [self showfirstphotoofgroup:album]; // (oreilly, programming ios 7, ch.17) nslog(@"a:%d",[_images count]); // return photo count if (!album) { nslog(@"%@", @"failed find album"); return; } } } ]; nslog(@"b:%d",[_images count]);
nslog a: correctly displays number of items in album 2.
nslog b: displays 0. in fact, gets processed before block!
i understand why - example apple has supplied. please tell me how can _images hold it's value outside block. block seems has different scope.
i've been trying load album array ages - block example doesn't hold it's value outside block!
the clue answer lies in own observation - nslog b processed before nslog a. method enumerategroupswithtypes:usingblock:failureblock
documented being asynchrnous, can return before has been enumerated. problem nothing values being retained etc., looking result before work done.
you have design application dependant on enumeration done after enumeration has completed. documentation enumerategroupswithtypes
states after enumeration finished block called once more , passed nil
, need test , use trigger dependant actions.
hth
Comments
Post a Comment