I have a Class called ServiceBrowser
. Inside this class I have a block based method that searches for NSNetServices.
I call the method as such :
[_serviceBrowser discoverServicesOfType:@"_theService._tcp."
inDomain:@"local."
didDiscoverServices:^(NSArray *services) {
NSLog(@"Services discovered %@", services);
[UIView fadeView:_button toAlpha:1.0 duration:0.5 completion:nil];
} didRemoveServices:^(NSArray *services) {
NSLog(@"Services removed %@", services);
} failure:^(NSString *message) {
NSLog(@"Failure %@", message);
}];
If I remove the call to fadeView:toAlpha:duration:completion:
it finds the services and logs them out. Only when I use self inside this block Xcode crashes without any error logged to the console.
fadeView:toAlpha:duration:completion:
is a category method on UIView that takes a view and fades it in or out and this works fine as a standalone method. The problem is when I use _button
inside the block it crashes.
I have investigated this and I assume it is down to a retain cycle. From looking at other questions and blog posts I should use a weak self inside the block.
I have tried using __block id weakSelf = self;
and also typeof(self) __weak w_self = self;
and neither work.