Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(816)

Unified Diff: webrtc/sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m

Issue 3013023002: Fix retain cycles in RTCUIApplicationStatusObserver. (Closed)
Patch Set: Reference weak self in initialization block. Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: webrtc/sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m
diff --git a/webrtc/sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m b/webrtc/sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m
index 168832d9ed08a2653f2595f4f425203bde3d5f7a..1d2aab48e852945fe25bd9d08a66ebb2fde9a0ac 100644
--- a/webrtc/sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m
+++ b/webrtc/sdk/objc/Framework/Classes/Common/RTCUIApplicationStatusObserver.m
@@ -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;
- }];
-
- [center addObserverForName:UIApplicationDidEnterBackgroundNotification
- 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;
+ }];
+
+ _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,
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698