objective c - ios: how to correctly handle request callback if view disappeared -


in view-controller load data server , call function render data or action when request data comes server. following snippet of such code.

- (void)viewwillappear:(bool)animated {     [super viewwillappear:animated];     __weak typeof(self) weakself = self;     [myrequestmanager loadobjects: ^(myobject* object, nserror* error) {         weakself.textfield.text = object.text;         //do other actions     }]; } 

if before data comes server view disappeared/unloaded, callback called? if called there chances of crash of unexpected things happen. how can prevent happening?

i can think of 1 way set variable in viewwillappear , viewwilldisappear. recommended way of doing this?

as you're keeping weakself inside, self won't retained, dealloced. should make weakself strong value inside, use strong 1 , won't crash.

__weak typeof(self) weakself = self; dispatch_group_async(_operationsgroup, _operationsqueue, ^{     typeof(self) strongself = weakself;     if (strongself) {         [strongself dosomething];         [strongself dosomethingelse];     } }); 

you may find more depth information eg here: http://albertodebortoli.github.io/blog/2013/08/03/objective-c-blocks-caveat/


Comments