Q:

ios: how to correctly handle request callback if view disappeared

In my view-controller usually I load some data from server and call some function to render data or do some action when request data comes back from server. following is the 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 some other actions
    }];
}

If before data comes from server view is disappeared/unloaded, will callback will be called? If it is called there are chances of crash of unexpected things to happen. So how can we prevent that from happening?
I can think of one way to set a variable in viewWillAppear and viewWillDisappear. What is recommended way of doing this?

A:

As you're keeping weakself inside, self won't be retained, so it will be dealloced. You should make your weakself a strong value inside, use the strong one and it 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/

