ios - When should I use weakself in a block, and why there is no retain cycle in Masonry? -
uibutton *testbutton = [[uibutton alloc] init]; [self.view addsubview:testbutton]; testbutton.backgroundcolor = [uicolor redcolor]; [testbutton mas_makeconstraints:^(masconstraintmaker *make) { make.width.equalto(@100); make.height.equalto(@100); make.left.equalto(self.view.mas_left); make.top.equalto(self.view.mas_top); }]; [testbutton bk_addeventhandler:^(id sender) { [self dismissviewcontrolleranimated:yes completion:nil]; } forcontrolevents:uicontroleventtouchupinside];
i use both blockskit , masonry in code. if use blockskit, bk_addeventhandler
, there retain cycle, think because self retain self.view, retain testbutton, retain self. however, when use mansonry alone without blockskit, , use strong self in masonry mas_makeconstraints
, occurs me there no retain cycle because viewcontroller can invoke dealloc method. can explain me there no retain cycle in masonry?
it's because block kit block retained execution @ later date (thus creating, thru reference self, retain cycle), whereas masonry block executed more or less right , thrown away.
similarly, never have worry retain cycle when call uiview animate...
method. it's because code ends, run loop ends , animation block executed , thrown away. reference self
in nsnotification observer block can cause retain cycle, because held forever system until unregister notification, , meanwhile retaining observer.
Comments
Post a Comment