I am new to blocks and while reading over the internet I found that I must use weak variables to blocks, because blocks retains the variables. I am little confuse while using self with blocks. lets have an example:
@interface ViewController : UIViewController
@property (copy, nonatomic) void (^cyclicSelf1)();
-(IBAction)refferingSelf:(id)sender;
-(void)doSomethingLarge;
@end
Here I have a ViewController and it has declared block property with copy attribute. I don't want to make a retain cycle so I know when using self in the block I need to create weak object of self eg:
__weak typeof(self) weakSelf = self;
What I want to make sure is my block executes on background thread and may be user hit back before it get finish. My block are performing some valuable task and I don't want that to loose. So I need self till the end of block. I did following in my implementation file:
-(IBAction)refferingSelf:(id)sender
{
__weak typeof(self) weakSelf = self; // Weak reference of block
self.cyclicSelf1 = ^{
//Strong reference to weak self to keep it till the end of block
typeof(weakSelf) strongSelf = weakSelf;
if(strongSelf){
[strongSelf longRunningTask];//This takes about 8-10 seconds, Mean while I pop the view controller
}
[strongSelf executeSomeThingElse]; //strongSelf get nil here
};
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), self.cyclicSelf1);
}
According to me, using typeof(weakSelf) strongSelf = weakSelf;
should create a strong reference of my self
and when user hit back, self will still have one strong reference inside block until the scope get over.
Please help me to understand why this get crash? Why my strongSelf is not holding the object.