As part of ResearchKit, I have seen code like this:
- (void)finishWithReason:(ORKTaskViewControllerFinishReason)reason error:(NSError *)error {
__strong typeof(self.delegate) strongDelegate = self.delegate;
if ([strongDelegate respondsToSelector:@selector(taskViewController:didFinishWithReason:error:)]) {
[strongDelegate taskViewController:self didFinishWithReason:reason error:error];
}
}
What's the purpose of storing a local strong variable pointing to the delegate before calling its method? Is this to prevent the delegate being freed from another thread between the respondsToSelector: check and the method call? Can this happen at all?
If that's the case, why would you want to call the delegate method? Why not let it be freed and perform the no-op that is sending a message to a nil pointer variable?
Aucun commentaire:
Enregistrer un commentaire