18
18
19
19
#if defined(__APPLE__)
20
20
21
+ #if TARGET_OS_IOS || TARGET_OS_TV
22
+ #import < UIKit/UIKit.h>
23
+ #endif
24
+
21
25
#include < SystemConfiguration/SystemConfiguration.h>
22
26
#include < dispatch/dispatch.h>
23
27
#include < netinet/in.h>
@@ -83,7 +87,7 @@ explicit ConnectivityMonitorApple(
83
87
return ;
84
88
}
85
89
86
- SCNetworkReachabilityFlags flags;
90
+ SCNetworkReachabilityFlags flags{} ;
87
91
if (SCNetworkReachabilityGetFlags (reachability_, &flags)) {
88
92
SetInitialStatus (ToNetworkStatus (flags));
89
93
}
@@ -107,9 +111,23 @@ explicit ConnectivityMonitorApple(
107
111
LOG_DEBUG (" Couldn't set reachability queue" );
108
112
return ;
109
113
}
114
+
115
+ #if TARGET_OS_IOS || TARGET_OS_TV
116
+ this ->observer_ = [[NSNotificationCenter defaultCenter ]
117
+ addObserverForName: UIApplicationWillEnterForegroundNotification
118
+ object: nil
119
+ queue: [NSOperationQueue mainQueue ]
120
+ usingBlock: ^(NSNotification * note) {
121
+ this ->OnEnteredForeground ();
122
+ }];
123
+ #endif
110
124
}
111
125
112
126
~ConnectivityMonitorApple () {
127
+ #if TARGET_OS_IOS || TARGET_OS_TV
128
+ [[NSNotificationCenter defaultCenter ] removeObserver: this ->observer_];
129
+ #endif
130
+
113
131
if (reachability_) {
114
132
bool success =
115
133
SCNetworkReachabilitySetDispatchQueue (reachability_, nullptr );
@@ -121,13 +139,37 @@ explicit ConnectivityMonitorApple(
121
139
}
122
140
}
123
141
142
+ #if TARGET_OS_IOS || TARGET_OS_TV
143
+ void OnEnteredForeground () {
144
+ SCNetworkReachabilityFlags flags{};
145
+ if (!SCNetworkReachabilityGetFlags (reachability_, &flags)) return ;
146
+
147
+ queue ()->Enqueue ([this, flags] {
148
+ auto status = ToNetworkStatus (flags);
149
+ if (status != NetworkStatus::Unavailable) {
150
+ // There may have been network changes while Firestore was in the
151
+ // background for which we did not get OnReachabilityChangedCallback
152
+ // notifications. If entering the foreground and we have a connection,
153
+ // reset the connection to ensure that RPCs don't have to wait for TCP
154
+ // timeouts.
155
+ this ->InvokeCallbacks (status);
156
+ } else {
157
+ this ->MaybeInvokeCallbacks (status);
158
+ }
159
+ });
160
+ }
161
+ #endif
162
+
124
163
void OnReachabilityChanged (SCNetworkReachabilityFlags flags) {
125
164
queue ()->Enqueue (
126
165
[this, flags] { MaybeInvokeCallbacks (ToNetworkStatus (flags)); });
127
166
}
128
167
129
168
private:
130
169
SCNetworkReachabilityRef reachability_ = nil ;
170
+ #if TARGET_OS_IOS || TARGET_OS_TV
171
+ id <NSObject > observer_ = nil ;
172
+ #endif
131
173
};
132
174
133
175
namespace {
0 commit comments