userMod2
userMod2

Reputation: 8960

Will NSUserDefaults remain if user quits app and are they global?

I'm using NSUserDefaults in my iOS app to record some specific info about the user's receipt state.

I'd like to confirm:

Upvotes: 1

Views: 112

Answers (4)

nugae
nugae

Reputation: 499

Two further points.

The uselessness of synchronize has grown up gradually through versions of iOS. It used to be essential; then advisable; then unnecessary. I can’t give you an exact timeline but a lot of it depends on how far back in iOS you want your app to work.

If you try to test some of these things in Xcode, beware. Up to and including iOS 11, synchronize does not write all the way to disk, but only puts data in a “lazy write” queue. Pressing the Stop button in Xcode (or pressing Run and allowing Xcode to stop the previous running app automatically) shuts everything down abruptly, more abruptly than anything a user can do, and the lazy writes are not written out, but lost. This is confusing while you are resting!! I filed a report on it and was told by Apple that (as Microfot would put it) “This behaviour is by design”.

But just to be clear: that second point does not present a problem for real apps in a real environment, only when you are trying to test “save and restart” scenarios. Waiting 5-10 seconds seems to be long enough for the flushed data to make it all the way to disk.

Upvotes: 0

Amit Pintz
Amit Pintz

Reputation: 21

From the NSUserDefaults documentation (see https://developer.apple.com/documentation/foundation/nsuserdefaults?language=objc)

With the exception of managed devices in educational institutions, a user’s defaults are stored locally on a single device, and persisted for backup and restore. To synchronize preferences and other data across a user’s connected devices, use NSUbiquitousKeyValueStore instead.

So for your first question the answer is yes.

Accordind to your second question, doc says:

At runtime, you use NSUserDefaults objects to read the defaults that your app uses from a user’s defaults database. NSUserDefaults 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.

And even so, it declares that the class is thread safe, so you can be sure about persistent results (for your second answer).

Upvotes: 2

Vinaykrishnan
Vinaykrishnan

Reputation: 768

Additionally with @Nikolai Ruhe answer.

if I set in method1 then later method2 I use the same line to get, it will have whatever I set in method1: NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

The UserDefaults class is thread-safe.

Upvotes: 0

Nikolai Ruhe
Nikolai Ruhe

Reputation: 81848

If the user quits the app, will those defaults remain?

Yes, they are persistent.

Are they global?

Global in the sense of your whole app: Yes.

Global in the sense of across apps: No. They are in the app's sandbox.

Upvotes: 4

Related Questions