5

Are operations on UserDefaults synchronous? For example does the second save wait for the first save to finish?

UserDefaults.standard.set(myValue1, forKey: "myValue1")
UserDefaults.standard.set(myValue2, forKey: "myValue2")
rmaddy
  • 307,833
  • 40
  • 508
  • 550
Micro
  • 9,771
  • 12
  • 75
  • 113
  • Also see https://stackoverflow.com/questions/40808072/when-and-why-should-you-use-nsuserdefaultss-synchronize-method for related details that may help clarify what's going on. – rmaddy May 07 '18 at 04:25
  • 1
    Yes it is `synchronous`. Also there is no function for saving with completion block. – TheTiger May 07 '18 at 04:36

1 Answers1

9

From apple's documentation:

At runtime, you use UserDefaults objects to read the defaults that your app uses from a user’s defaults database. UserDefaults caches the information to avoid having to open the user’s defaults database each time you need a default value. When you set a default value, it’s changed synchronously within your process, and asynchronously to persistent storage and other processes.

M Reza
  • 16,461
  • 14
  • 61
  • 67