-
Notifications
You must be signed in to change notification settings - Fork 24.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NetInfo.isConnected.addEventListener behaves incorrectly #22966
Comments
@mmmulani you've worked on RCTNetInfo most recently. Any thoughts here? |
I've mainly done crash fixes on RCTNetInfo, not any actual feature improvement. @karanjthakkar can you take a look at this? |
@mmmulani Thanks for tagging me. @benjarwar I tested out your example and it seems like this issue has been happening for atleast the last 6 commits, which was before I touched this piece of code. Now I know this isn't the answer you are hoping for, but I don't have the bandwidth to debug the reason behind this falkiness atm. If you are sure it was working on some version of RN in the past, then the only thing I can recommend is to git bisect from the point to now and figure out which commit broke it. That will help someone who would be willing to look into this 🙂 |
@karanjthakkar thanks for taking a look. I'll try to triage further. |
Simulator or device? When i switch wifi on an off on laptop connectionChange still only fires once in simulator, but this has been working pretty reliably for us in 57.8 on device. (I actually thought this was fixed, appears still an issue though since when I remove the hacky code our offline status check goes bonkers.) Edit: hacky code removed, no longer needed? |
I have similar problems on NetInfo too. The listener behaves unpredictably even when there are changes on the phone status like when it goes in stand-by. |
@nol13 interesting. In testing this further now, it seems not to be occurring now on my actual device (I swear it was previously!), but still consistently occurs in the iOS Simulator. So is this a known problem? Are there other issues or documentation about If anything, it behooves a note in the documentation, especially for such a critical feature. I'll try out your hacky solution. It feels a little redundant and brittle to call Also, what's up with needing to check the network status on app state change? Have you encountered inaccuracies when the app comes back into focus? This sounds like what @HighSoftWare96 is describing. |
Ya, that's what the app state check was for. It would sometimes be offline for a split second right after unlocking, but the event wouldn't fire when it went back on. The double check is because only the second check was reliable, and even then sometimes (every few days) it needed a bit of a timeout as well. That code was originally to work around NetInfo as it existed in 55.3, but after the 57.8 upgrade didn't notice any issues so figured I'd post what we had. I suspect the double check and AppState check may not be needed anymore. I'll edit post to avoid confusion. And ya, I tried last night with just.. _checkNetworkStatus = (isConnected) => {
this.setNetworkStatus(isConnected);
} and it completely blew up on me even on device, but worked fine when I did a single NetInfo.isConnected.fetch(). yet now just tried again and above seems to be working without issue :/ |
@nol13 your code doesn't seem to help in the Simulator. The problem seems to be with the What's interesting, though, is if I add a check for I've updated my I still feel like this functionality needs some attention. These "solutions" are flaky and non-deterministic. Example code:
|
I have the same problem but only for Android devices, isConnected returns false if the device wakes up after 10-15 minutes (but no sooner than 5-10 mins). |
I'm having the same issue and tried @benjarwar's workaround of using a Right now, what I had to do was, within the callback I remove the event listener when it gets fired. Then I re-add it a few seconds later with a I'm on react-native version 0.50.4 which is a little over a year old now. I'm just implementing this now so I can't say whether it worked before or not. But I really doubt that this has been broken for over a year. I wonder if it's an emulator issue since it works on the device 100%? Emulator version 10.0 (SimulatorApp-851.2 CoreSimulator-581.2). |
Heads up: we moved react-native-netinfo into its own repository, which should allow for making changes and fixes much faster. Please continue the discussion about this issue there: react-native-netinfo/react-native-netinfo#7 |
Environment
Description
The callback passed to
NetInfo.isConnected.addEventListener
does not fire consistently nor accurately in iOS,0.57.8
. We cannot reliably determine whether or not the user is connected when following the documented method.This is an enormous problem on our project, where we're trying to restrict requests and functionality when the user is offline.
Reproducible Demo
I created a test app with
react-native init
:https://github.com/benjarwar/NetInfoTest
Simply added a
NetInfo.isConnected
event listener that console logs the connection status onconnectionChange
:https://github.com/benjarwar/NetInfoTest/blob/master/App.js#L21-L25
Testing locally in iOS, on initial load it logs
true
. When I disable WiFi, it correctly logsfalse
. But thereafter I get incorrect behavior:true
but then immediately/incorrectly logsfalse
afterwards with WiFi still onThe text was updated successfully, but these errors were encountered: