Skip to content
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

Android: Timers do not run if specified before App mounts #22089

Closed
3 tasks done
EdwardDrapkin opened this issue Nov 2, 2018 · 6 comments
Closed
3 tasks done

Android: Timers do not run if specified before App mounts #22089

EdwardDrapkin opened this issue Nov 2, 2018 · 6 comments
Labels
Bug Platform: Android Android applications. Stale There has been a lack of activity on this issue and it may be closed soon.

Comments

@EdwardDrapkin
Copy link

EdwardDrapkin commented Nov 2, 2018

Environment


  React Native Environment Info:
    System:
      OS: macOS High Sierra 10.13.6
      CPU: x64 Intel(R) Core(TM) i7-4770HQ CPU @ 2.20GHz
      Memory: 40.69 MB / 16.00 GB
      Shell: 4.4.23 - /usr/local/bin/bash
    Binaries:
      Node: 10.12.0 - /var/folders/7r/_djt8b5s0tn_c2dmpdcwhjn40000gn/T/yarn--1541193013040-0.8521692356694159/node
      Yarn: 1.12.1 - /var/folders/7r/_djt8b5s0tn_c2dmpdcwhjn40000gn/T/yarn--1541193013040-0.8521692356694159/yarn
      npm: 6.4.1 - ~/.nvs/node/10.12.0/x64/bin/npm
      Watchman: 4.9.0 - /usr/local/bin/watchman
    SDKs:
      iOS SDK:
        Platforms: iOS 11.4, macOS 10.13, tvOS 11.4, watchOS 4.3
      Android SDK:
        Build Tools: 23.0.1, 25.0.3, 26.0.2, 26.0.3, 27.0.3, 28.0.1
        API Levels: 23, 24, 25, 26, 27, 28
    IDEs:
      Xcode: 9.4.1/9F2000 - /usr/bin/xcodebuild
    npmPackages:
      react: ^16.6.0 => 16.6.0
      react-native: ^0.57.3 => 0.57.4

Description

If you call setTimeout or setInterval as the very first thing in the application, before any calls to react-native itself, they will never run. They do in iOS.

Reproducible Demo

Wrap your startup AppRegistry call in a setTimeout:

setTimeout(() => AppRegistry.registerComponent(appName, () => App), 100);
@react-native-bot react-native-bot added the Platform: Android Android applications. label Nov 2, 2018
@bartolkaruza
Copy link

Just out of curiosity, what exactly is the use case for this? I thought the registerComponent call needed to be within the first event loop of the app, which is not the case if you wrap it in a setTimeout. I'm not sure if it is related to the issue, but there is a particular error thrown if the app is not registered before the runApplication is called. Can you check your adb logcat output for that error?

@EdwardDrapkin
Copy link
Author

Nothing happens, like ever. No log messages or errors, the function just never gets called. It gets called immediately if you set the timeout to 0, however.

We have to make a network request to determine which content pane to show users on app launch based on their remote session state... since NetInfo is also broken before React Native launches on Android I was trying to use setTimeout to handle slow network requests. On iOS, we can use NetInfo before React-Native launches to immediately display a "network required" error message and exit the application; on Android, I was trying to find a way to avoid launching all of react-native if the network request was going to fail anyway.

I'm going to have to work around this by launching the application into a headless component of some sort that can use NetInfo and it is going to greatly increase the complexity of the app boot process.

iOS boot looks something like: (await NetInfo.isConnected.fetch()) ? showApp() : showWarning() and Android boot is gonna have to do that all of that in an otherwise useless component's componentDidMount() unless there's a better way to ensure network connectivity before launching the app?

@bartolkaruza
Copy link

bartolkaruza commented Nov 6, 2018

There are two considerations for moving this logic into an actual component instead of pre registerComponent, UX and semantics:
UX is the situation where NetInfo returns connected, but you are not able to actually reach your server. Think of the "walled garden" state of wifi hotspots or other network issues while the device thinks it is connected. In those cases you would want to display some loading indication while your request is loading (and the request to your server is timing out), followed by displaying the warning on the actual timeout.
The second, semantics, is that you are considering registerComponent as startApp, which is not what registerComponent means; it actually registers a particular React component to a specific namespace. Therefore it should be done as early as possible and the application logic should be moved until after this point.
This is just my opinion on how you can avoid this issue entirely, I'm not saying there might not be an actual inconsistency between Android and iOS. I think you might not want to worry about that, if you simply change the order of initialisation to a more stable post registerComponent approach.

@EdwardDrapkin
Copy link
Author

All of that is entirely in place already... our app is used frequently in retail stores to finance sales, so we wanted to short-circuit the loading timeout if there was no network whatsoever to save customers' time (potentially standing in line / with a retail rep). I wound up just mounting a headless component that does the check in componentDidMount(). It's probably a frame or two slower, but imperceptibly, so we'll be shipping the check in the next release. I just figured that since I had noticed the bug in the first place, might as well report it. People might wind up here from google and see that it's (un)fixed and know whether to start considering a workaround (which, admittedly, would have saved me an hour).

@stale
Copy link

stale bot commented Aug 2, 2019

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Aug 2, 2019
@stale
Copy link

stale bot commented Aug 9, 2019

Closing this issue after a prolonged period of inactivity. If this issue is still present in the latest release, please feel free to create a new issue with up-to-date information.

@stale stale bot closed this as completed Aug 9, 2019
@facebook facebook locked as resolved and limited conversation to collaborators Aug 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Platform: Android Android applications. Stale There has been a lack of activity on this issue and it may be closed soon.
Projects
None yet
Development

No branches or pull requests

4 participants