Fix retain cycles in RTCUIApplicationStatusObserver.
These retain cycles are theoretical since the singleton is supposed to live for the lifetime of the application. These measures were removed earlier when the object was turned into a singleton in a previous CL, see https://chromium-review.googlesource.com/c/external/webrtc/+/527442/3..4/webrtc/sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m The weak self handling and unused dealloc method is mostly noise and can make a casual reader think that the object will have a limited life cycle, i.e. the code may initially look like something it is not, which could possibly be less readable. On the other hand, for people looking out for potential retain cycles, the code may be distracting since it looks like it may be leaking. BUG=b/65558647 Review-Url: https://codereview.webrtc.org/3013023002 Cr-Commit-Position: refs/heads/master@{#19811}
This commit is contained in:
@ -16,12 +16,25 @@
|
||||
|
||||
#include "webrtc/rtc_base/checks.h"
|
||||
|
||||
@interface RTCUIApplicationStatusObserver ()
|
||||
|
||||
@property(nonatomic, assign) BOOL initialized;
|
||||
@property(nonatomic, assign) UIApplicationState state;
|
||||
|
||||
@end
|
||||
|
||||
@implementation RTCUIApplicationStatusObserver {
|
||||
BOOL _initialized;
|
||||
dispatch_block_t _initializeBlock;
|
||||
UIApplicationState _state;
|
||||
|
||||
id<NSObject> _activeObserver;
|
||||
id<NSObject> _backgroundObserver;
|
||||
}
|
||||
|
||||
@synthesize initialized = _initialized;
|
||||
@synthesize state = _state;
|
||||
|
||||
+ (instancetype)sharedInstance {
|
||||
static id sharedInstance;
|
||||
static dispatch_once_t onceToken;
|
||||
@ -35,24 +48,27 @@
|
||||
- (id)init {
|
||||
if (self = [super init]) {
|
||||
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
||||
[center addObserverForName:UIApplicationDidBecomeActiveNotification
|
||||
object:nil
|
||||
queue:[NSOperationQueue mainQueue]
|
||||
usingBlock:^(NSNotification *note) {
|
||||
_state = [UIApplication sharedApplication].applicationState;
|
||||
}];
|
||||
__weak RTCUIApplicationStatusObserver *weakSelf = self;
|
||||
_activeObserver = [center addObserverForName:UIApplicationDidBecomeActiveNotification
|
||||
object:nil
|
||||
queue:[NSOperationQueue mainQueue]
|
||||
usingBlock:^(NSNotification *note) {
|
||||
weakSelf.state =
|
||||
[UIApplication sharedApplication].applicationState;
|
||||
}];
|
||||
|
||||
[center addObserverForName:UIApplicationDidEnterBackgroundNotification
|
||||
object:nil
|
||||
queue:[NSOperationQueue mainQueue]
|
||||
usingBlock:^(NSNotification *note) {
|
||||
_state = [UIApplication sharedApplication].applicationState;
|
||||
}];
|
||||
_backgroundObserver = [center addObserverForName:UIApplicationDidEnterBackgroundNotification
|
||||
object:nil
|
||||
queue:[NSOperationQueue mainQueue]
|
||||
usingBlock:^(NSNotification *note) {
|
||||
weakSelf.state =
|
||||
[UIApplication sharedApplication].applicationState;
|
||||
}];
|
||||
|
||||
_initialized = NO;
|
||||
_initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^{
|
||||
_state = [UIApplication sharedApplication].applicationState;
|
||||
_initialized = YES;
|
||||
weakSelf.state = [UIApplication sharedApplication].applicationState;
|
||||
weakSelf.initialized = YES;
|
||||
});
|
||||
|
||||
dispatch_async(dispatch_get_main_queue(), _initializeBlock);
|
||||
@ -61,6 +77,12 @@
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)dealloc {
|
||||
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
|
||||
[center removeObserver:_activeObserver];
|
||||
[center removeObserver:_backgroundObserver];
|
||||
}
|
||||
|
||||
- (BOOL)isApplicationActive {
|
||||
if (!_initialized) {
|
||||
long ret = dispatch_block_wait(_initializeBlock,
|
||||
|
||||
Reference in New Issue
Block a user