I've heard that I should always use weakSelf
in blocks to avoid retain cycles, but what about dispatch blocks? In this case, my method handles an error response from my server in the following code:
//handling server errors (particularly "Token Refresh Failed" ones)
-(void)handleServerErrorResponse:(NSString *)error {
dispatch_async(dispatch_get_main_queue(), ^{
UIAlertController *alertController = [DialogHelper getAlertForSimpleAuthError:error];
if ([error isEqualToString:@"Your login session has expired"]) {
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action)
{
[MyModelDataCenter emptyDataCenter];
[MyAPIInterface sharedInstance].authToken = nil;
NSUserDefaults* defaults = [NSUserDefaults standardUserDefaults];
[defaults removeObjectForKey:@"authToken"];
[defaults removeObjectForKey:@"defaultUserObjectDictionary"];
[defaults synchronize];
[AuthenticationHelper sharedInstance].loggedInUser = nil;
[self.navigationController popToRootViewControllerAnimated:YES];
}]];
}
else {
[alertController addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:nil]];
}
[self presentViewController:alertController animated:YES completion:nil];
});
}
Should I be using weakSelf
in this block the same as I do it in other blocks?