Nativescript iOS long runnning background task


I am working on an iOS application (in nativescript) that needs to get and post some data to an API in the background each 5 minutes. Now it seems that iOS is closing my application so I took a look at the long running tasks section of but I fail to see how I should configure an interval now.

Also while it works correctly on the simulator (not thinking about the interval) with the debug fetch command, I can't get it to work correctly on a real device.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Owen RubinConsultantCommented:
Is iOS closing the app itself, or both the app and the background task as well?

My guess is your debug fetch command does little, so it runs fine. But is it possible that the real fetch command is causing a crash?
Have you tried just running the fetch command in foreground using a timer for now and seeing if that works first?

Have a look at this Stack Overflow article on using timers:

It is old, but has an update for Swift 4.

If the fetch works in the foreground, then you might need to post your code to see what is crashing.

Have you looked at the logs to see if it is crashing?
gonniegijsenAuthor Commented:
Well I implemented the long running tasks but it didn't trigger. Only after 24 hours the background task started doing what it should, did it a couple of minutes later again, then it was gone again for another 24 hours or something. The NSTimer solution worked great, until after +- 3 minutes when iOS closed the task. What I need is something like the alarm manager on android
Owen RubinConsultantCommented:
So if the task is “closing}, I suspect it is crashing. And you know how difficult it can be debugging a background task.  I will assume you have no exit calls in your app.  I think it’s back to iOS’s logs to see if you can find what caused the task to exit.

I have not used alarm manager on Android. So not sure it’s function. Can you not just create an alarm event for exact time and let iOS handle the timing and alarm? (I have never tried that.)

I have only sec NSTimer in an Objective-c app, and it was used in a music player to update the song playing info.

Not sure what else I can suggest. You might consider posting the code to stackoverflow and get a programmer there to help you.
Maximize Customer Retention with Superior Service

The IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more to help build customer satisfaction and retention.

gonniegijsenAuthor Commented:
In it says that iOS will close it, so that might be why the NSTimer is suddenly stopped
Owen RubinConsultantCommented:
Hmmm. I guess that is fairly new then. One of iOS's problems in the past was that if an app crashed, it could leave background talks running. And since one task cannot effect another, it was almost impossible to kill. (Why iPhones used to get slow over time and burn the battery faster. A factory reset and restore was the only way to fix.

So perhaps they finally do proper cleanup on background tasks. BUT, if your app is still active, the timer should keep running. I have used that in a music player I built to check for graphics updates, and my timers run until I kill them on exit.
Owen RubinConsultantCommented:
Just looked at that page.  Suspend not kill. That is not quite the same, but I forgot about that. Your main app should get a call back to a suspend method just before that is going to happen. Might be able to do something to stop suspend. I have never looked at doing that. Sorry.

You also have to act on a timer event obviously. Perhaps your timer trigger event can kill the current timer and start a new one. Not sure, but that just might handle the suspend problem. (But if the host app exits, I assume your exit method cleans up and delete all tuners, right?)

My app was a music player so it had the plist settings to keep it running in the background.That is one of the few types if apps that stay active.

Not sure this will get by Apple but I have seen people register their app as a music player, and then just play a short 1 second of silence ( a quiet 1 second sound sampme) to keep the task active as a "music" player. Not kosher, but might work for testing. It would obviously interfere with other sound generating apps and music, so not really a production idea. But for testing, it can keep you active for testing other things.

Also, you can use the plist to register as a network app, and they too are allowed to run in background longer. Check the docs on this as I have not done that in a long time.
gonniegijsenAuthor Commented:
Well if it's not going to pass apple, i cant really do it.. I have a feeling that what I want to achieve is not possible with iOS. The closest I had was setting the UIBackgroundModes to fetch but I can't control the launche/resume time.
Owen RubinConsultantCommented:
I'm looking at one more thing. Stand by a short while.
gonniegijsenAuthor Commented:
So I've been waiting a couple of days now, did you find anything ?
Owen RubinConsultantCommented:
Sorry, no. I was out of the country for two weeks so I could not spend a lot of time on it. Sorry.
gonniegijsenAuthor Commented:
I guess it's not really possible sadly, thanks for your help

Can be closed
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.