react-native icon indicating copy to clipboard operation
react-native copied to clipboard

Allow network requests to finish when app is sent to background

Open maxkomarychev opened this issue 4 years ago • 3 comments

Summary

This change allows network requests to be executed in background safely.

In certain conditions iOS HTTP requests are being terminated with this error:

Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."

While working on this issue I discovered this happens when app is brought to background (sometimes several times).

Changelog

[iOS] [Fixed] - Fix random network errors happening when application is in background

Test Plan

  1. Create sample express server:

    const express = require('express');
    const app = express();
    const port = 9999;
    
    app.get('/', (req, res) => {
      setTimeout(() => {
        res.send('Hello World!');
      }, 20000);
    });
    
    app.listen(port, () => {
      console.log(`Example app listening at http://localhost:${port}`);
    });
    
  2. Run the following code in react-native app:

    const App = () => {
      return (
        <View style={{margin: 30}}>
          <Button
            onPress={() => {
              console.log('sending a request');
              axios
                .get('http://192.168.0.110:9999')
                .then(r => {
                  console.log('response', r.data);
                })
                .catch(e => {
                  console.log('error', e);
                });
            }}
            title="REQUEST"
          />
        </View>
      );
    };
    
  3. Hit "REQUEST" button and send app to background few times for a few seconds each. The request should succeed in the end.

maxkomarychev avatar Jul 09 '21 08:07 maxkomarychev

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,969,468 +11
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 9,561,974 -13
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: eddefec5f8dc58a1ecf66944e36d8c780fac7064 Branch: main

analysis-bot avatar Aug 11 '21 19:08 analysis-bot

PR build artifact for 807ad5bea4e1209e09b41fa1ae336aa72cbf7988 is ready. To use, download tarball from "Artifacts" tab in this CircleCI job then run yarn add <path to tarball> in your React Native project.

pull-bot avatar Oct 29 '21 08:10 pull-bot

@maxkomarychev Thank you for this! It should get merged in.

We need this for android as well right?

rossmartin avatar Nov 06 '22 20:11 rossmartin

kind bump here 👋

maxkomarychev avatar Sep 13 '23 13:09 maxkomarychev

Looks like e2e jobs are failing for reasons unrelated to the code. What can be done to fix this? image

maxkomarychev avatar Sep 14 '23 13:09 maxkomarychev

This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Mar 13 '24 05:03 github-actions[bot]

This PR is stale because it has been open 180 days with no activity. Remove stale label or comment or this will be closed in 7 days.

This pr is still relevant. Can it be looked at please? I'm not sure how others are dealing with the fact that network requests hang up when the app goes to background. We have to carry patch with "patch package" in all our apps because of this problem.

maxkomarychev avatar Mar 13 '24 21:03 maxkomarychev

i am so sad about this problem..... hope this pr should merge....

dodokw avatar Jun 19 '24 07:06 dodokw