I understand that the DeviceActivityMonitor extension is designed to be very lightweight, and the system terminates the extension as soon as its callback functions return.
However, I want to save values to UserDefaults
inside the extension's callback functions. This creates concurrency issues for my app because the documentation for UserDefaults
states that "When you set a default value, it’s changed synchronously within your process, and asynchronously to persistent storage and other processes."
In order to guarantee that these values are persisted before the extension terminates my app, I want to call UserDefaults.synchronize(), but its documentations states that it's "unnecessary and shouldn't be used." Furthermore, it's listed under "Legacy" but not marked deprecated.
Is synchronize()
the recommended way to solve my concurrency problem? Or could there be a better way to wait for storage synchronization before returning from a synchronous function?