¿Es realmente necesario el baile debself / strongSelf cuando se hace referencia a uno mismo dentro de un final no retenido llamado desde un UIViewController?
Digamos que tengo el siguiente método dentro de unUIViewController
subclase
- (void)makeAsyncNetworkCall
{
[self.networkService performAsyncNetworkCallWithCompletion:^{
dispatch_async(dispatch_get_main_queue(), ^{
[self.activityIndicatorView stopAnimating];
}
});
}];
}
Sé que la referencia aself
dentro del bloque resulta en elUIViewController
La instancia queda retenida por el bloque. MientrasperformAsyncNetworkCallWithCompletion
no almacena el bloque en una propiedad (o ivar) en miNetworkService
¿Tengo razón al pensar que no hay ciclo de retención?
Me doy cuenta de que esta estructura anterior llevará a que se retenga el controlador UIViewController hastaperformAsyncNetworkCallWithCompletion
Termina, incluso si es liberado por el sistema anterior. Pero es probable (o incluso posible) que el sistema desasigne miUIViewController
en absoluto (Después de los cambios en la forma en que iOS 6 gestiona unUIViewController
el respaldoCALayer
memoria)?
Si hay una razón por la que debo hacer la "danza débil / fuerte / fuerte", se vería así:
- (void)makeAsyncNetworkCall
{
__weak typeof(self) weakSelf = self;
[self.networkService performAsyncNetworkCallWithCompletion:^{
typeof(weakSelf) strongSelf = weakSelf;
if (!strongSelf) {
return;
}
dispatch_async(dispatch_get_main_queue(), ^{
[strongSelf.activityIndicatorView stopAnimating];
}
});
}];
}
Pero me parece muy desagradable y me gustaría evitarlo si no es necesario.