I have the following function
private func SetupLocaleObserver ()
{
NotificationCenter.default.addObserver (
forName: NSLocale.currentLocaleDidChangeNotification,
object: nil,
queue: .main
) {_ in
print ("Locale changed to: \(Locale.current.identifier)");
}
}
I call this function inside the viewDidLoad ()
method of my view controller. The expectation was that whenever I change the system or app-specific language preference, the locale gets changed, and this change triggers my closure which should print "Locale changed to: <new locale>" on the console.
However, the app gets terminated with a SIGKILL whenever I change the language from the settings. So, it is observed that sometimes my closure runs, while most of the times it does not run - maybe the app dies even before the closure is executed.
So, the question is, what is the use of this particular notification if the corresponding closure isn't guaranteed to be executed before the app dies? Or am I using it the wrong way?
Great question!
Changes to the Preferred Languages or App Language almost always cause the app to be quit, as these changes can cause the Bundle
to get reloaded, which necessitates the process to be relaunched in order for the change to take effect. In these cases, your app may not receive the currentLocaleDidChangeNotification
beforehand.
The cases in which you would receive currentLocaleDidChangeNotification
is when you go into Language & Region settings and change other properties such as Calendar, Temperature Unit, etc. which can change without necessitating apps to be relaunched.
Hope this helps!