3

Is it ok if i do something like:

  -(void)example{
       __weak __typeof__(self) weakSelf = self;

       dispatch_queue_t dispatchQueue = dispatch_queue_create("q_getRestaurants", NULL);
       dispatch_async(dispatchQueue, ^{

           dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf doSomething];
           });
       });
    }



   -(void)doSomething{
       //can i use self inside this method????
       self.view.backgroundColor = [UIColor redColor];
   }

The thing is that i wanna know if there is ok if i use self inside the do something method that is being called from a queue that has a weakSelf call.

Ponja
  • 663
  • 1
  • 8
  • 15
  • Are you saying that during the "//Call something from server" section you want to reference `self`? – Guy Kogus Mar 20 '14 at 15:57
  • no, the thing is if i can call [weakSelf doSomething] and use **self** inside the **doSomething** method – Ponja Mar 20 '14 at 16:01
  • 2
    Yes, you can. Inside a method (other than init) you are guaranteed that self is always a valid value. If self had been deallocated, and weakSelf automatically set to nil, then `[weakSelf doSomething];` will instead do nothing and not actually invoke doSomething. – David Berry Mar 20 '14 at 16:03
  • This question could be interesting for you http://stackoverflow.com/questions/22339490/arc-self-and-blocks/ – Avt Mar 20 '14 at 16:09
  • @David: Well, you are guaranteed that `self` (like all parameters) points to a valid value *at the beginning of* a method. Not later. – newacct Mar 21 '14 at 02:41
  • @newacct since assigning to self is only allowed in init (the compiler gives an error anywhere else), you have to really work to invalidate self within a method. – David Berry Mar 21 '14 at 15:41
  • @David: Assigning to `self` is not the issue. The issue is that the object pointed to by `self` can be deallocated. – newacct Mar 21 '14 at 18:50
  • @newacct in an ARC world, I think you'd have to be doing something incredibly bizarre for self to be invalidated during a method invocation. I made an extremely contrived test here http://pastebin.com/wk86FYmx and self is still valid, even after deleting all known references (other than the method invocation) Perhaps you can come up with an example? – David Berry Mar 21 '14 at 19:50
  • @David: It doesn't have to be bizarre or contrived. Here is a very reasonable situation where the object pointed to by `self` becomes invalid in the middle of the method: http://pastebin.com/fZkXuzYR – newacct Mar 24 '14 at 09:38

1 Answers1

4

Yes. You're good there. Only variables inside the block itself are retained.

Dave Wood
  • 13,143
  • 2
  • 59
  • 67
  • 1
    So, no matters if I use self inside the **doSomething** method if I call it from the weakSelf pointer? – Ponja Mar 20 '14 at 16:03
  • Correct. Though you might want to consider making a strongSelf inside the block to ensure the weakSelf doesn't get nilled out. Depends on what self is though. If it's an appDelegate or something you don't need to worry since it's not going anywhere. – Dave Wood Mar 20 '14 at 16:05