0

I have several actions to perform before the application quits. They take about 3 or 4 seconds to be done (and may be more, up to 10 secs), and when I try to execute them in applicationWillTerminate, only a part of them are done.

I also tried executing them in applicationShouldTerminate before return NSApplicationTerminateReply.TerminateNow, but still some of them are not performed. How to do all of them and then quit properly?

pomo_mondreganto
  • 2,028
  • 2
  • 28
  • 56
  • Are you sure your tasks only take three to four seconds? According to [the Apple developer library](https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIApplicationDelegate_Protocol/index.html?hl=de#//apple_ref/occ/intfm/UIApplicationDelegate/applicationWillTerminate:), using the applicationWillTerminate method, "Your implementation of this method has approximately five seconds to perform any tasks and return." – Benjamin Lowry Feb 09 '16 at 12:15
  • 1
    @BenjaminLowry, actually, now I'm looking for the solution that will allow to run even more code (approximately 8-9 seconds) – pomo_mondreganto Feb 09 '16 at 12:18
  • Are you absolutely certain the tasks need it to be done when the app is terminated and not just in the background? – Benjamin Lowry Feb 09 '16 at 12:19
  • @BenjaminLowry, yes. It's like the clean-up before app quits – pomo_mondreganto Feb 09 '16 at 12:20
  • I have read around and it seems to be the consensus that using _applicationDidEnterBackground_ is the safest place to do app clean ups since _applicationWillTerminate_ is only called when the app is terminated while in the foreground. Thus it is safer to always perform the clean ups when your user even just leaves the app. This should also solve your timing issues. – Benjamin Lowry Feb 09 '16 at 12:23
  • @BenjaminLowry, and if user has never left app window? The function won't be called, and I'll still need to perform tasks in `applicationShouldTerminate` – pomo_mondreganto Feb 09 '16 at 12:29
  • Fair enough. I do not know of any ways to elongate the _applicationWillTerminate_ time, so my biggest suggestion is to cut down your processes. – Benjamin Lowry Feb 09 '16 at 12:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102995/discussion-between-benjamin-lowry-and-nikitin-roman). – Benjamin Lowry Feb 09 '16 at 12:38
  • Did you try `TerminateLater` and `replyToApplicationShouldTerminate`? – Willeke Feb 09 '16 at 13:39

1 Answers1

0

This would be helpful. Handle all of your clean up actions here

applicationShouldTerminate(_ sender: NSApplication) -> NSApplicationTerminateReply {
    // your tear down logic 
}
flash76
  • 382
  • 3
  • 23
CodeLab
  • 11
  • 1