react-native-background-fetch: [Question] Timing out an async call

It is required behaviour on iOS to call the finish event within 30sec of starting the background fetch event. However, it is thinkable that my async call to the server does not receive a response (result or error) from the server within 30 seconds and the code keeps waiting, leading to the app being killed. Intuitively I would have used a javascript timeout (setTimeout), which calls the finish event after say 25 seconds. However, the timeout does not seem to work in background mode and the function defined in the timeout is only called once the app returns to foreground. How do you work around this issue? Or am I just not understanding something?

Your Environment

  • Plugin version: 2.4.6
  • Platform: both
  • OS version: android 8, iOS 12
  • Device manufacturer / model: Samsung a3, iPhone SE
  • React Native version (react-native -v): 0.57.8
  • Plugin config

Expected Behavior

function defined in setTimeout is called after the defined time in background mode

Actual Behavior

function defined in setTimeout is called only when app returns to foreground

Steps to Reproduce

  1. Define a timeout with setTimeout within the background fetch config
  2. Move app to background
  3. Fire a background fetch event
  4. Wait… After waiting long enough, move app back to foreground, see function inside timeout being called

Context

Timeout async server calls so that iOS doesnt kill the app if server returns nothing within 30sec

Debug logs

N/A

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 26 (11 by maintainers)

Most upvoted comments

Any recommendations on how to deal with uploads

Decrease your upload size. Apple called this api “fetch”, meaning it’s designed for fetching data from your server (eg latest scores for a sports app).

uploading large files is not what it was designed for.

You call #finish when your task is done. If you’re going an async call, you need to call #finish when your async call is “finished”.

#finish is a signal to the native OS that “I am finished. You can end the task now and allow the OS to suspend again”

What makes you think your app is being terminated because of this? Do you have crashlog in XCode device logs to show this?

The plugin has a failsafe to automatically #finish a task when it’s nearing expiration. You don’t need to deal with timers.