question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] Timing out an async call

See original GitHub issue

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

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
christocracycommented, Dec 13, 2021

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.

1reaction
christocracycommented, Sep 1, 2019

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”

Read more comments on GitHub >

github_iconTop Results From Across the Web

Asynchronously wait for Task<T> to complete with timeout
@TomexOu: the question was how to asynchronously await a Task's completion. Task. Wait(timeout) would synchronously block instead of asynchronously await.
Read more >
c# - Async task with timeout - Code Review Stack Exchange
I am calling a service outside of my control. My application must include a time out, so that if the call to the...
Read more >
setTimeout() - Web APIs - MDN Web Docs
The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.
Read more >
How to get asynchronous task's result with timeout in Java
This method optionally takes a timeout period during which it waits for the result. The method throws an exception if the wait is...
Read more >
Running an async task with a timeout - Swift Forums
Though using a deadline does not really solve the problem by itself, because if you're unlucky the timeout task might be severely delayed...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found