8 Replies
      Latest reply on Dec 3, 2019 5:14 PM by nvannote
      nvannote Level 1 Level 1 (0 points)

        macOS 10.14.6 (Mojave)

        Xcode 11.2.1 (11B500)

        Language: Objective-C

         

        An out of the box macOS application does not call the delgate applicationWillTerminate.

         

        I have logs in both applicationDidFinishLaunching and applicationWillTerminate in the default source.

         

        - (void)applicationDidFinishLaunching:(NSNotification *)notification {
        
            NSLog(@"applicationDidFinishLaunching");
        }
        
        - (void)applicationWillTerminate:(NSNotification *)notification {
        
            NSLog(@"applicationWillTerminate");
        }

         

        applicationDidFinishLaunching logs fine; but applicationWillTerminate does not get executed at all while quiting.  Again this is a default app generation for macOS (No options/Objective-C).

         

        Known issue?  Bug?

         

        Thanks,

         

        Neil

        • Re: applicationWillTerminate not being called
          PBK Level 7 Level 7 (3,365 points)

          Does applicationDidEnterBackground get called?

          When restarting does applicationWillEnterForeground get called or does application:didFinishLaunchingWithOptions get called?

          Why do you think your app 'terminates'?

           

          Are you familiar with:

          https://developer.apple.com/documentation/uikit/app_and_environment/managing_your_app_s_life_cycle?language=objc

            • Re: applicationWillTerminate not being called
              nvannote Level 1 Level 1 (0 points)

              Thanks PBK,  As per my post; this is a macOS application (not iOS).  Yes, the default generated App terminates and UIKit is not a factor.  Unfortunately, applicationWillTerminate never gets called so we can correctly clean up server connections and whatnot.

               

              Again, I created a brand new "out of the box" application to confirm.  In both Objective-C (and Swift); applicationWillTerminate is not called.  NSLog and Breakpoints applied.  applicationDidFinishLaunching gets executed.

               

              Regards,

               

              Neil

                • Re: applicationWillTerminate not being called
                  Claude31 Level 8 Level 8 (7,255 points)

                  I observed the same in a Swift project, with some strange behavior.

                  Conf: OSX 10.14.6 - XCode 11.2 ß2 - Swift

                   

                  I created an empty OSX project, just adding a print in willTerminate

                      func applicationWillTerminate(_ aNotification: Notification) {
                          print("Will terminate")
                      }

                   

                   

                  When I quit the app, nothing printed.

                   

                  I added a WKWebView in the main window, with the following code in the ViewController:

                  import Cocoa
                  import WebKit
                  
                  class ViewController: NSViewController {
                  
                      @IBOutlet weak var webView: WKWebView!
                      override func viewDidLoad() {
                          super.viewDidLoad()
                  
                          // Do any additional setup after loading the view.
                          let url = URL(string: "https://www.apple.com")
                          let request = URLRequest(url:url!)
                          webView.load(request)
                      }
                  
                      override var representedObject: Any? {
                          didSet {
                          // Update the view, if already loaded.
                          }
                      }
                  
                  }

                  I set the entitlements for out connections and ran

                   

                  I got the log printed.

                   

                  I started again, making incremental changes from the empty project

                  never printed until I loaded the request.

                   

                  I commented out

                       //   webView.load(request)

                   

                  No more print.

                   

                  Cannot figure out what is going on here.

                   

                  Bug report filed: Nov 30, 2019 at 5:37 PM – FB7473098

                   

                  EDITED:

                  Tested as well with ObjC: willTerminate does not print log on an "empty" app.

                  • Re: applicationWillTerminate not being called
                    sstanley Level 2 Level 2 (40 points)

                    `Xcode 11 seems to be including `Application can be killed immediately when user is shutting down or logging out` in apps' Info.plist with a value of YES, which will result in what you see. Set it to NO, or remove it.

                      • Re: applicationWillTerminate not being called
                        Claude31 Level 8 Level 8 (7,255 points)

                        In my case, this is set to YES and there's no crash. As well as under special circumstances.

                        With XCode 11.2ß2 and XCode 11.2.1

                         

                        "Application can be killed to reclaim memory or other exceptional circumstances

                        <?xml version="1.0" encoding="UTF-8"?>

                        <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

                        <plist version="1.0">

                        <true/>

                        </plist>

                         

                        Application can be killed immediately when user is shutting down or logging out

                        <?xml version="1.0" encoding="UTF-8"?>

                        <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">

                        <plist version="1.0">

                        <true/>

                        </plist>

                         

                        But that may behave different on mvannote Mac ?

                        • Re: applicationWillTerminate not being called
                          nvannote Level 1 Level 1 (0 points)

                          Yep, that did the trick, thanks for that.  BUT  "Application can be killed immediately when user is shutting down or logging out" does not apply in my case.  All I am doing is quiting the app, NOT "shutting down or logging out";  This is obviously a huge bug and should not be the default for macOS.

                           

                          Regards,

                           

                          Neil

                            • Re: applicationWillTerminate not being called
                              Claude31 Level 8 Level 8 (7,255 points)

                              There may be some other root cause.

                               

                              In my case, those properties were set to YES and I did not get any crash.

                               

                              Did you file a bug ?

                                • Re: applicationWillTerminate not being called
                                  nvannote Level 1 Level 1 (0 points)

                                  Well, In my original post, there wasn't a "crash" but just a straight out applicationWillTerminate not getting called at all without notice.  This method being called (when expected) is really kind of important for applications that may have uncommitted resources hanging out in the breeze.

                                   

                                  The verbiage "Application can be killed immediately when user is shutting down or logging out" seems to indicate when the user is shutting down or logging out of the machine/account;  Not just normally quitting an application.

                                   

                                  Bug will be filed in the next day or two.

                                   

                                  - Neil