Mac Background Scheduler

Hey there,


I wanted to use this to create a background task for my MacOS App.


Unfortunately I don't knwo how to insert the code.

I created


let activity = NSBackgroundActivityScheduler(identifier: "com.example.MyApp.work")


in AppDelegate and insert


activity.repeats = true
activity.interval = 30


in applicationDidFinishLaunching() to initialize the values.



But where is the right place to insert


activity.schedule() { (completion: NSBackgroundActivityScheduler.CompletionHandler) in
            // Perform the activity
            print("Hello World")
            self.completion(NSBackgroundActivityScheduler.Result.Finished)
        }


in that way that this application will get a background task, which is not stopped if I quit (or leave) the application?


Maybe that's an sutpid question but if so it sould be easy to answer ;-)


Thanks a lot!

which is not stopped if I quit (or leave) the application?

That’s not how

NSBackgroundActivityScheduler
works. Rather, it assumes that your app is running and then it runs associated block when it’s a good time to perform the activity. This is helpful, for example, if you need to index a big database but want to avoid doing that while the user is actively using the Mac.

If you want this activity to run after the user has quit your app, you should move that work to a login item so that it’s always running.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

ps DTS is closed 21 Dec through 1 Jan.

The idea with the login item is really good.


I'll try to find instructions how to implement, if I'm not succesfull I'll contact you another time.


Best regards

I'll try to find instructions how to implement [a login item]

There are many different ways to implement a login item. The approach I recommend is that one shown by the AppSandboxLoginItemXPCDemo sample code.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Mac Background Scheduler
 
 
Q