Skip to content

Commit 5a16cee

Browse files
authored
Checks for connectivity when App is moving to foreground. (#4985)
* Checks for connectivity when App is moving to foreground. * Split MaybeInvokeCallbacks and invoke them when foregrounding and network is available.
1 parent 180f9e5 commit 5a16cee

File tree

3 files changed

+51
-2
lines changed

3 files changed

+51
-2
lines changed

Firestore/core/src/firebase/firestore/remote/connectivity_monitor.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,12 @@ void ConnectivityMonitor::MaybeInvokeCallbacks(NetworkStatus new_status) {
3333
if (new_status == status_) {
3434
return;
3535
}
36-
status_ = new_status;
3736

37+
InvokeCallbacks(new_status);
38+
}
39+
40+
void ConnectivityMonitor::InvokeCallbacks(NetworkStatus new_status) {
41+
status_ = new_status;
3842
for (auto& callback : callbacks_) {
3943
callback(status_.value());
4044
}

Firestore/core/src/firebase/firestore/remote/connectivity_monitor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ class ConnectivityMonitor {
7171
// Invokes callbacks only if the status changed.
7272
void MaybeInvokeCallbacks(NetworkStatus new_status);
7373

74+
// Invokes callbacks and sets net status to `new_status`.
75+
void InvokeCallbacks(NetworkStatus new_status);
76+
7477
const std::shared_ptr<util::AsyncQueue>& queue() {
7578
return worker_queue_;
7679
}

Firestore/core/src/firebase/firestore/remote/connectivity_monitor_apple.mm

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818

1919
#if defined(__APPLE__)
2020

21+
#if TARGET_OS_IOS || TARGET_OS_TV
22+
#import <UIKit/UIKit.h>
23+
#endif
24+
2125
#include <SystemConfiguration/SystemConfiguration.h>
2226
#include <dispatch/dispatch.h>
2327
#include <netinet/in.h>
@@ -83,7 +87,7 @@ explicit ConnectivityMonitorApple(
8387
return;
8488
}
8589

86-
SCNetworkReachabilityFlags flags;
90+
SCNetworkReachabilityFlags flags{};
8791
if (SCNetworkReachabilityGetFlags(reachability_, &flags)) {
8892
SetInitialStatus(ToNetworkStatus(flags));
8993
}
@@ -107,9 +111,23 @@ explicit ConnectivityMonitorApple(
107111
LOG_DEBUG("Couldn't set reachability queue");
108112
return;
109113
}
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
110124
}
111125

112126
~ConnectivityMonitorApple() {
127+
#if TARGET_OS_IOS || TARGET_OS_TV
128+
[[NSNotificationCenter defaultCenter] removeObserver:this->observer_];
129+
#endif
130+
113131
if (reachability_) {
114132
bool success =
115133
SCNetworkReachabilitySetDispatchQueue(reachability_, nullptr);
@@ -121,13 +139,37 @@ explicit ConnectivityMonitorApple(
121139
}
122140
}
123141

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+
124163
void OnReachabilityChanged(SCNetworkReachabilityFlags flags) {
125164
queue()->Enqueue(
126165
[this, flags] { MaybeInvokeCallbacks(ToNetworkStatus(flags)); });
127166
}
128167

129168
private:
130169
SCNetworkReachabilityRef reachability_ = nil;
170+
#if TARGET_OS_IOS || TARGET_OS_TV
171+
id<NSObject> observer_ = nil;
172+
#endif
131173
};
132174

133175
namespace {

0 commit comments

Comments
 (0)