You’re now watching this thread. If you’ve opted in to email or web notifications, you’ll be notified when there’s activity. Click again to stop watching or visit your profile to manage watched threads and notifications.
You’ve stopped watching this thread and will no longer receive emails or web notifications when there’s activity. Click again to start watching.
How do I handle force quit in Swift? I received crash reports during a tesflight test. I didn't understand what it was: none of my app's symbols were present, and Xcode didn't want them either... unlike two others who were very specific.
By doing a few Google queries, I saw that [UIApplication _terminateWithStatus:] + 136 (UIApplication.m:7578). Accompanied by a SIGSEV... corresponded to a simple thing: it's a crash during a force quit.
I tested it with two iPhones, connected to my Mac, and launched the app from Xcode on each of them. I waited a bit, then quit it. It immediately went into the background and waited to launch operations with BackgroundTaskManager. I went to the app carousel and quit it with a swipe of my finger. I immediately see in the log that "sceneDidDisconnect" from SceneDelegate is called... then the immediate crash occurs, with the same elements as those received during the test flight
: SIGSEV and [UIApplication _terminateWithStatus:] and identical elements thereafter.
And this applies regardless of what I put in "SceneDidDisconnect," a print, and something to close the BGtasks if they are running (but iOS should normally kill them too, right?) .. 1 or 2 secondes after it crashes.
At the moment of the crash, the Xcode cursor is positioned on "class Appdelegate" in AppDelegate.
My question is: how do I handle force quit in Swift? I have another Objective-C application that does the same thing and runs identical operations in the background. If I force quit it, there is no crash.
And this applies regardless of what I put in "SceneDidDisconnect," a print, and something to close the BGtasks if they are running (but iOS should normally kill them too, right?) .. 1 or 2 secondes after it crashes.
My general recommendation is that you specifically avoid doing any kind of "system" clean up on the close/exit path. That's because:
For things like writing data out to disk, your implicitly assuming that your app will always exit in a "clean" way. That's another way of saying "Please destroy data when my app crashes".
For things like memory, the system can destroy things faster than you can.
Many of our frameworks (case in point, BackgroundTask) heavily rely on IPC to one or more supporting daemons, which makes reliably "cleanup" impossible. In many cases your app will have already terminate before the daemon actually acts on whatever message you sent.
Related to that point, keep in mind that the system HAS to be able to safely clean up it's own resources regardless of what your app does.
Case in point:
...something to close the BGtasks if they are running (but iOS should normally kill them too, right?)
The system will take care of this because NOT taking care of this would mean that system resources are leaked/mishandled anytime an app crashes.
If i put "exit(0) " in SceneDiddisconnect, the crash doesnot appear , The problem is in AppDelegate. But what problem ? I have put this code : this is not called.
Is the point where UIApplication specifically calls "applicationWillTerminate" in your application delegate. So, my guess would be that your AppDelegate object is actually being deallocated, probably by something your SceneDelegate is doing.
If i put "exit(0) " in SceneDiddisconnect, the crash doesnot appear , The problem is in AppDelegate. But what problem ? I have put this code : this is not called.
And this applies regardless of what I put in "SceneDidDisconnect," a print, and something to close the BGtasks if they are running (but iOS should normally kill them too, right?) .. 1 or 2 secondes after it crashes.
My general recommendation is that you specifically avoid doing any kind of "system" clean up on the close/exit path. That's because:
For things like writing data out to disk, your implicitly assuming that your app will always exit in a "clean" way. That's another way of saying "Please destroy data when my app crashes".
For things like memory, the system can destroy things faster than you can.
Many of our frameworks (case in point, BackgroundTask) heavily rely on IPC to one or more supporting daemons, which makes reliably "cleanup" impossible. In many cases your app will have already terminate before the daemon actually acts on whatever message you sent.
Related to that point, keep in mind that the system HAS to be able to safely clean up it's own resources regardless of what your app does.
Case in point:
...something to close the BGtasks if they are running (but iOS should normally kill them too, right?)
The system will take care of this because NOT taking care of this would mean that system resources are leaked/mishandled anytime an app crashes.
If i put "exit(0) " in SceneDiddisconnect, the crash doesnot appear , The problem is in AppDelegate. But what problem ? I have put this code : this is not called.
Is the point where UIApplication specifically calls "applicationWillTerminate" in your application delegate. So, my guess would be that your AppDelegate object is actually being deallocated, probably by something your SceneDelegate is doing.
You gave me a direction to search : SceneDelegate. In "WillConnect to" , There was a line :
UIApplication.shared.delegate = self
i removed this line, rebuild with no error, what was this line doing here ? I do not remember.. And test this version. Put it in back ground , wait , force-kill ... "sceneDidDisconnect" is executed AND applicationWillTerminate in appDelegate too.