Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fetch data in background every hour in Swift even the app is terminated?

I am making an app that provides functionality to fetch data every hour. It fetches data even when the app terminates. How can I implement this functionality?

After a lot searching I found background fetching using performFetchWithCompletionHandler. Will this function work even if my app terminates? What I mean is if my app is in closed or terminated state, can I call this function automatically?

like image 456
Jack.Right Avatar asked May 19 '16 09:05

Jack.Right


People also ask

How do you run an application continuously running in the background Swift?

You need to enable Background Mode in your project settings under capabilities tab. Under background modes you will find a few modes that satisfy various purposes of running an app in background.

Will iOS terminate the app running in background after a specific time?

At the same time, didReceiveMemoryWarning is invoked for the app. At this point, so that your app continues to run properly, the OS begins terminating apps in the background to free some memory. Once all background apps are terminated, if your app still needs more memory, the OS terminates your app.

What is background app refresh in Swift?

Background app refresh is a feature of iOS and Android that allows apps to update their content from the internet, even while you're not using them. In contrast, we say that apps use data in the foreground when you open and use them yourself.

How can we execute code when app is not running?

If you need to execute code when your app isn't running, there are several options open to you depending on what you're trying to do. Background fetch will let your app run in the background for about 30 seconds at scheduled intervals. The goal of this is to fetch data and prepare your UI for when the app runs next.


2 Answers

You can't do anything if your app is in terminated state. Apple is not allowing anything (except receiving notification) in terminated or closed state.

You are also restricted for background execution also. You can perform specific kind of task in background mode like : Location update,Music playing, VOIP, finite length task, Newsstand downloads, etc. You can refer apple documentation for more details about background execution.

Hope this will help :)

like image 148
Ketan Parmar Avatar answered Sep 19 '22 18:09

Ketan Parmar


Once I needed this same when I was working on some project, unfortunately I didn't find any solution for it.

You can run a task when your app is in Background state(that too with limited services) but apple doesn't allow to run a task when app is Terminated

Please go through the link.

https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

and this is for background running. Sending Latitude and Longitude to Server when app is in background

like image 36
Santo Avatar answered Sep 17 '22 18:09

Santo