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

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

Issue 3003633002: ObjC: Always dispatch async in UIApplication status observer. (Closed)
Patch Set: Created 3 years, 4 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 unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright 2016 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2016 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "RTCUIApplicationStatusObserver.h" 11 #include "RTCUIApplicationStatusObserver.h"
12 12
13 #if defined(WEBRTC_IOS) 13 #if defined(WEBRTC_IOS)
14 14
15 #import <UIKit/UIKit.h> 15 #import <UIKit/UIKit.h>
16 16
17 @implementation RTCUIApplicationStatusObserver { 17 @implementation RTCUIApplicationStatusObserver {
18 BOOL _initialized;
19 dispatch_block_t _initializeBlock;
18 UIApplicationState _state; 20 UIApplicationState _state;
19 } 21 }
20 22
21 + (instancetype)sharedInstance { 23 + (instancetype)sharedInstance {
22 static id sharedInstance; 24 static id sharedInstance;
23 static dispatch_once_t onceToken; 25 static dispatch_once_t onceToken;
24 dispatch_once(&onceToken, ^{ 26 dispatch_once(&onceToken, ^{
25 sharedInstance = [[self alloc] init]; 27 sharedInstance = [[self alloc] init];
26 }); 28 });
27 29
(...skipping 10 matching lines...) Expand all
38 _state = [UIApplication sharedApplication].applicationStat e; 40 _state = [UIApplication sharedApplication].applicationStat e;
39 }]; 41 }];
40 42
41 [center addObserverForName:UIApplicationDidEnterBackgroundNotification 43 [center addObserverForName:UIApplicationDidEnterBackgroundNotification
42 object:nil 44 object:nil
43 queue:[NSOperationQueue mainQueue] 45 queue:[NSOperationQueue mainQueue]
44 usingBlock:^(NSNotification *note) { 46 usingBlock:^(NSNotification *note) {
45 _state = [UIApplication sharedApplication].applicationStat e; 47 _state = [UIApplication sharedApplication].applicationStat e;
46 }]; 48 }];
47 49
48 dispatch_block_t initializeBlock = ^{ 50 _initialized = NO;
51 _initializeBlock = dispatch_block_create(DISPATCH_BLOCK_INHERIT_QOS_CLASS, ^ {
49 _state = [UIApplication sharedApplication].applicationState; 52 _state = [UIApplication sharedApplication].applicationState;
50 }; 53 _initialized = YES;
54 });
51 55
52 if ([NSThread isMainThread]) { 56 dispatch_async(dispatch_get_main_queue(), _initializeBlock);
53 initializeBlock();
54 } else {
55 dispatch_sync(dispatch_get_main_queue(), initializeBlock);
56 }
57 } 57 }
58 58
59 return self; 59 return self;
60 } 60 }
61 61
62 - (BOOL)isApplicationActive { 62 - (BOOL)isApplicationActive {
63 if (!_initialized) {
64 dispatch_block_wait(_initializeBlock, DISPATCH_TIME_FOREVER);
daniela-webrtc 2017/08/23 08:54:10 This will make the 'isApplicationActive' a blockin
andersc 2017/08/23 09:32:26 Before, we blocked on initialization by doing a di
kthelgason 2017/08/23 11:37:39 Maybe instead of blocking forever we should add a
andersc 2017/08/23 12:14:22 That sounds like a good idea! Done.
65 }
63 return _state == UIApplicationStateActive; 66 return _state == UIApplicationStateActive;
64 } 67 }
65 68
66 @end 69 @end
67 70
68 #endif // WEBRTC_IOS 71 #endif // WEBRTC_IOS
OLDNEW
« 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