0

I'm trying to call a function when my macOS application is in any state, including terminated. Here is what i'm trying to accomplish:

Schedule a function (much like DispatchQueue.main.asyncAfter()) to run daily at a given time (let's say 9AM). I would like to add a feature to my application that allows a user to pick a time of day, and have an Alamofire POST request run at that time every day.

I have tried using a Runloop, and more recently Grand Central Dispatch:

    DispatchQueue.main.asyncAfter(wallDeadline: DispatchWallTime.now() + .seconds(60)) {
        //Alamofire
    }

I can easily accomplish this while the application is running with a timer, but have yet to find a way to accomplish this in the background, with the app running.

jscs
  • 63,694
  • 13
  • 151
  • 195
Ckacmaster
  • 366
  • 1
  • 10

1 Answers1

1

This may be pretty heavy to implement (i.e. not straightforward), but if you want a task to run even if your app is terminated, you might need to consider writing your own LaunchAgent.

The trick here would be for the agent to be able to interact with your application (retrieving or sending shared information).

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • Thanks for the info, I was hoping to avoid making a launch agent, for the reason you mentioned, however i'll try doing that and see where I end up. Thanks. – Ckacmaster Apr 07 '18 at 00:01