Break retain cycle for background tasks

This commit is contained in:
Jerry Beers 2012-10-30 22:29:11 -05:00 committed by Mattt Thompson
parent 652ad787e7
commit c3bc36abd7

View file

@ -257,15 +257,16 @@ 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;
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{ self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
if (handler) { if (handler) {
handler(); handler();
} }
[self cancel]; [_blockSelf cancel];
[application endBackgroundTask:self.backgroundTaskIdentifier]; [application endBackgroundTask:_blockSelf.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid; _blockSelf.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}]; }];
} }
[self.lock unlock]; [self.lock unlock];