react-native
react-native copied to clipboard
Allow network requests to finish when app is sent to background
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
-
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}`); }); -
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> ); }; -
Hit "REQUEST" button and send app to background few times for a few seconds each. The request should succeed in the end.
| 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
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.
@maxkomarychev Thank you for this! It should get merged in.
We need this for android as well right?
kind bump here 👋
Looks like e2e jobs are failing for reasons unrelated to the code. What can be done to fix this?
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 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.
i am so sad about this problem..... hope this pr should merge....