Using conventional weak/strong self pattern to break retain cycle in background tasks
This commit is contained in:
parent
953dfbcfbc
commit
4b2c2818a1
1 changed files with 9 additions and 5 deletions
|
|
@ -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];
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue