Nativescript iOS long runnning background task

gonniegijsen
gonniegijsen used Ask the Experts™
on
Hi,

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 https://docs.nativescript.org/core-concepts/ios-runtime/how-to/BackgroundExecution 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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
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: https://stackoverflow.com/questions/24007518/how-can-i-use-nstimer-in-swift

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? http://help.dayoneapp.com/troubleshooting/getting-ios-crash-logs

Author

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

Commented:
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.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
In https://github.com/NativeScript/sample-ios-background-execution it says that iOS will close it, so that might be why the NSTimer is suddenly stopped

Commented:
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.

Commented:
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.

Author

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.

Commented:
I'm looking at one more thing. Stand by a short while.

Author

Commented:
So I've been waiting a couple of days now, did you find anything ?

Commented:
Sorry, no. I was out of the country for two weeks so I could not spend a lot of time on it. Sorry.

Author

Commented:
I guess it's not really possible sadly, thanks for your help

Can be closed

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial