Inside a ViewController with ARC (.m file has compiler flags in Build Phases -fobjc-arc -fobjc-arc-exceptions)
As I know apple recommend to use weak ref for self inside a block like this
self __weak typeof(self) weakSelf = self;
In some case weakSelf is nil inside block, I have few e.g.
e.g.1
__weak typeof(self) weakSelf = self;
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:lat longitude:lon];
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//some logic here using weakSelf
=> weakSelf nil
}
e.g.2
__block id weakSelf = self;
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:lat longitude:lon];
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//some logic here using weakSelf
=> weakSelf nil
}
e.g. 3
__weak typeof(self) weakSelf = self;
__block id safeBlockSelf = self;
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:lat longitude:lon];
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error) {
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//some logic here using safeBlockSelf
=> weakSelf NOT nil
}
Some Ideas why weakSelf is nil?