0

I am an iOS developer and I have developed an app that runs a periodic background task using Background Modes, I want to implement the same feature in the MacOS version of this app, as I am new to the MacOS development, I am facing some issue regarding implementation of this feature.

I have used NSBackgroundActivityScheduler to schedule a periodic activity, which works fine, when the app is running or is in the dock. But I failed to achieve the same result , when I quit the application. I want to run that task even after the application terminates or quits.

func startBackgroundTask() {

  let activity =  NSBackgroundActivityScheduler.init(identifier:"com.abc.xyz")
  activity.invalidate()
  activity.interval = 60
  activity.repeats = true
  activity.qualityOfService = QualityOfService.background
  activity.schedule() {(completion: NSBackgroundActivityScheduler.CompletionHandler) in

  print("background task scheduled")

  // Perform the activity
  NotificationManager.sharedManager().showNotification(informativeText: "Next notification will come after 1 minute") //This is to find notify the background service started.

  //perform operation over here...

  completion(NSBackgroundActivityScheduler.Result.finished)
}

I have researched various options regarding this , like XPC , LaunchAgent , LoginItems , Process etc. but I am confused which option to go with. As on apple developer forum they have used XPC with LoginItems, but I got stuck when I tried to implement it.

Anshuman Singh
  • 1,018
  • 15
  • 17
  • This kinda depends on what exactly the nature of the task is. you can use launchd to run a job on a schedule. you can also keep a background process running after your GUI app quits. lots of options. It also depends on if you need the job to run when your user is not logged in. – Brad Allred Sep 13 '18 at 22:29
  • I have enabled the app to be launched at the time of login. but when the user quits the GUI, after that I am unable to perform the task. So I want to run a periodic task even after the GUI app quits. – Anshuman Singh Sep 17 '18 at 07:27
  • if it doesnt require elevated permission and needs to run on a per user basis then a LaunchAgent set to a schedule is probably the best route to go. However, if what your schedule is actually doing is _polling_ something then maybe its better you rediesign to not require polling and instead have an XPC service that listens on some socket. your question lacks actual requirements/context. – Brad Allred Sep 17 '18 at 15:02
  • @BradAllred my requirement is to update the contents in the database , periodically after some time(i.e 2 mins), even after the user quits the GUI. So I have created a Scheduler which will run periodically, is there any way to call this scheduler even after the app gets quit?. – Anshuman Singh Sep 18 '18 at 17:15
  • Yes, as I said, a LaunchAgent (launchd) can do just that. – Brad Allred Sep 18 '18 at 18:48
  • @BradAllred I have executed the LaunchAgent, but I have to load it manually through terminal, and also I am unable to access the database of the main application from the LaunchAgent target. Is there any way to do so? – Anshuman Singh Sep 19 '18 at 06:37
  • FFS RTFM http://www.manpagez.com/man/5/launchd.plist/ look at the `StartInterval` and `StartCalendarInterval` keys. your other problem is another question you should open and ask. – Brad Allred Sep 19 '18 at 17:02

0 Answers0