Background threads ends automatically after 3 minutes

Hi,


Whenever i am creating any thread to run in background for any application it stops automatically after 3 minutes. I am using Xamarin forms for building the iOS application. The issue exists only with the iOS and is working fine with other platforms. Below here is the code that i used to create the background thread:


nint taskID = UIKit.UIApplication.SharedApplication.BeginBackgroundTask( () => {Task.Factory.StartNew (() => syncData ());});


I have tried enabling the background mode and background fetch attributes in info.plist but using these could not solve the issue.


Please suggest me for the above issue.

Replies

iOS puts strict limits on the amount of time that general-purpose apps can run in the background. When the user moves an app to the background, the system typically suspends that app after a few seconds. You can use a UIApplication background task to delay that suspension for a few minutes, but you can’t delay it indefinitely.

There’s no way to prevent this suspension in the general case. Apps can operate in the background for longer, but such operation is always tied to some specific user-visible behaviour. What sort of work are you trying to do in the background?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hi eskimo,


I am trying to fetch some information via thread in background for infinite time. The main purpose of the thread is to update some data in local database and update the UI of the application accordingly.


Also for the user visible behaviour i had provided the local notification after 1 minute of interval, but it closes automatically after 3 mins. Thus, i am searching for some way to run this thread for infinite time in background.


Further, if it is not possible to run it for infinite time then to which max time period can i run it in background.

I am trying to fetch some information via thread in background for infinite time.

Fetch from where?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

I am trying to fetch data from API.


Also i am trying the "Perfrom Fetch with completionHandler" (override method) provide by ios and also enabling the background fetch. Here i set minimumbackgroundFetchTime of 15 mins but it never fired.


It is only working when i use ios debuger for iphone and ios simulated background fetch debugger for simulator.

The system resumes (or relaunches) your app in the background to do background fetches (

-application:performFetchWithCompletionHandler:
) based on its own internal criteria. The exact mechanics of this is not documented but you can learn more about the general approach by watching WWDC 2016 Session 204 What’s New with Multitasking. A key point here is that background fetch requests are (partly) determined by user activity, and thus the only way to really see it in action is to use your app like a real user would.

Oh, one thing I haven’t mentioned yet: it is common for your background fetch code to start tasks in an NSURLSession background session. The session will then resume (or relaunch) your app when those tasks are complete. So, a typical background fetch app will actually use two different ‘resuming in the background’ features:

  • background fetch, to kick off the network requests

  • NSURLSession background sessions, to process their results

You can also use more. For example, if your app talks to a service that always updates its results at a particular time, you can use ‘silent’ push notifications to start the app after the service has updated. This is commonly used by magazine style apps, which use a silent push to tell the clients that today’s content is available.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"