I am using fetch
to perform an API call on React Native but as soon as the app is put in the background, the request fails/is cancelled. How can I let the request finish?
Edit: I have tried to add the 'Background processing' background mode capability in xcode and that didn't help.
Edit 2: I tried switching to axios for the request and that didnt help.
export const performAPICall = async (endpoint, body) => {
const backendURL = "https://example.com/api";
return await axios.post(backendURL+'/'+endpoint, body, {
headers: {
'Content-Type': 'application/json',
"Authorization": "Bearer "+store.getState().nav.accessToken
}
})
.then((res) => res.text())
.then(
(result) => {
result = JSON.parse(result);
return result;
},
(error) => { // <--- This is where it hits when starting an API call then putting the app in the background
// [AxiosError: Network Error] is the error
console.log("performAPICall error", error);
return null;
}
)
.catch(
(error) => {
console.log("API CALL CATCH", error);
return null;
}
);
}