2

Assuming self is also subclass of UIViewController, I do understand that all the UI methods of self, need to be performed inside main thread.

Consider the code given below:

- (void)someMethodWhichMayOrMayNotBeInMainThread {

    // perform some function

    MyController * __weak weakSelf = self;
    dispatch_async(dispatch_get_main_queue(), ^{
        MyController *strongSelf = weakSelf;
        if (!strongSelf) {
            return;
        }
        [strongSelf updateListView];
    });

}

In this case, if I do not give a weak reference to self, will it make a difference?

This method in itself may be called asynchronously, so the self in it might or might not be a strong reference to the weak reference to self.

Now, say the self is deallocated in the main thread already, then even if I do not assign the self in this method weakself, will it crash?

uraimo
  • 19,081
  • 8
  • 48
  • 55
Avnish Gaur
  • 489
  • 1
  • 4
  • 16
  • 1
    possible duplicate of [Using weak self in dispatch\_async function](http://stackoverflow.com/questions/21987067/using-weak-self-in-dispatch-async-function) – uraimo Jun 15 '15 at 11:39
  • And no, it will not crash. – uraimo Jun 15 '15 at 11:40
  • @uraimo But in the given question, he is using self which will ensure that it is retained till the block completes.. but i am using a strong reference to the weak reference to self.. will it not make any difference? – Avnish Gaur Jun 15 '15 at 12:21
  • Check the first answer, there is a nice explanation. – uraimo Jun 15 '15 at 12:41

0 Answers0