What is the maximum amount of data that UserDefaults can store?

Asked 2 years ago, Updated 2 years ago, 44 views

Is it okay to put endless data into UserDefaults?I'm not sure if I should switch to Sqlite or Realm.
It would be better to centralize the database, but it would be troublesome if it were an existing application.I use it because it's convenient, but of course, if I put it in endlessly, I think it will be ruined somewhere.

I would like to confirm the following two points.

  • What is the maximum capacity
  • Does more capacity cause frequent errors, slow loading and writing?

Please let me know if there are any anti-patterns if you put a large amount of data in UserDefaults and reject it.
Currently, my app contains serialized object data (list data), image objects, etc.

swift ios xcode

2022-09-30 19:11

1 Answers

Can I put endless data into UserDefaults?

It's not good.

I can't decide whether to switch to Sqlite or Realm.

Why doesn't it contain Core Data that can be used in iOS standards?By the way, using SQLite directly from the code is not recommended because it is not productive and buggy.

  • What is the maximum capacity

Apple documents include:

sizeLimitExceededNotification

Currently, there is only a size limit for data stored to local user defaults on tvOS, which posts a warning notification when user defaults storage reach 512kBin size, and terminals apps when user defaults storage reach 1MB in size.

(Currently, only tvOS has a size limit.)

Increased capacity can cause frequent errors, slow loading and writing.

  • It depends on how often you think it happens, so it's a bit far away, but you can find articles like "App crashes because you put too much data into UserDefaults."

NSUserDefaults Crash

In the case of this article, it says, "It takes a long time to process, so I killed the app, and now the app crashes at startup."

  • Yes for
  • Late loading and writing

For example, there is a blog post that looks at the details of UserDefaults' behavior.

UserDefaults Limitations and Alternatives

(I gave it to you because it is representative and easy to understand, but I think you will find many other similar articles.All the articles I write after looking into them carefully are almost the same content and conclusion.)

  • UserDefaults are stored together in a single plist file
  • Of course, if it gets bigger, it takes time to load.
    (plist needs to load the entire file even if it wants only one piece of data)
  • Of course, it takes time to write when it gets bigger
    (plist also needs to rewrite the entire file when updating only one piece of data)
  • Once loaded, plist files are cached in memory, which also compresses memory

There's nothing good about storing a lot of data in UserDefaults.

Please let me know if you have any anti-patterns if you put a large amount of data in UserDefaults.

Putting a lot of data into UserDefaults without having to eat them is a typical anti-pattern.Don't do that with a proper, authentic app, let alone an introductory programming article for beginners.

The blog post above summarizes when to use UserDefaults as follows:

  • UserDefaults is best used for parsing very small user preference type data which needs to be restored from backup.
  • Anything else should be stored in the appropriate directory depending on how long the data is needed and where or not it can be regenerated or re-downloaded.

("UserDefaults is good for perpetuating a small amount of data, such as user preference" or "Anything else should be stored in the appropriate directory considering its characteristics")

In the past, Apple's Developer Forums used to receive almost the same response from Apple technicians, but recently I couldn't find the link, perhaps because there are fewer questions that seem to be using UserDefaults inappropriately.

Currently, my app contains serialized object data (list data), image objects, etc.

This is my personal opinion, and it's a criterion for developing apps that I can separate, but

  • Serialized object data (list data)

Yellow cards, data that allows users to store any number of data (like tens of thousands or tens of thousands of data quickly if users continue to use the app for a long time), cannot be included in UserDefaults

  • Image Object

Red card, data that grows in size even by just one image will never be included in UserDefaults

It's like that.For your information.


2022-09-30 19:11

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.