Using conventional weak/strong self pattern to break retain cycle in background tasks

This commit is contained in:
Mattt Thompson 2012-10-31 07:49:44 -07:00
parent 953dfbcfbc
commit 4b2c2818a1

View file

@ -257,16 +257,20 @@ static inline BOOL AFStateTransitionIsValid(AFOperationState fromState, AFOperat
[self.lock lock]; [self.lock lock];
if (!self.backgroundTaskIdentifier) { if (!self.backgroundTaskIdentifier) {
UIApplication *application = [UIApplication sharedApplication]; UIApplication *application = [UIApplication sharedApplication];
__unsafe_unretained typeof(self) _blockSelf = self; __weak __typeof(&*self)weakSelf = self;
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{ self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
__strong __typeof(&*weakSelf)strongSelf = weakSelf;
if (handler) { if (handler) {
handler(); handler();
} }
[_blockSelf cancel]; if (strongSelf) {
[strongSelf cancel];
[application endBackgroundTask:_blockSelf.backgroundTaskIdentifier];
_blockSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid; [application endBackgroundTask:strongSelf.backgroundTaskIdentifier];
strongSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}
}]; }];
} }
[self.lock unlock]; [self.lock unlock];