4

I know that NSUserDefaults isn't ideal for storing a large amount of data, but I don't know what the actual limit is. Which made me thinking ...

1. Is there a recommended size limit?

2. Is there a way to see the size of the data I'm storing?

In this case, I'm thinking of using NSUserDefaults to store an array of strings.

Christofer Vilander
  • 17,232
  • 6
  • 31
  • 26

2 Answers2

8

It appears the limit is the maximum file size for iOS (logically), which is currently 4GB: https://discussions.apple.com/thread/1763096?tstart=0. The precise size of the data is circumscribed by the compiler types (NSData, NSString, etc.) or the files in your asset bundle.

I am sure there is low-level documentation on Swift types somewhere--but you will need to discover it yourself. :)

I have not redirected to another answer because similar questions also suggested that the limit is the device size.

Be aware that NSUserDefaults is considered harmful for large files, because all defaults are loaded immediately at application launch. Access time will be slow, performance will be impacted. Consider using a structured storage medium, such as Core Data, CloudKit, or SQLite, for large files.

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CoreData/nsfetchedresultscontroller.html#//apple_ref/doc/uid/TP40001075-CH8-SW1

https://developer.apple.com/icloud/

http://www.appcoda.com/sqlite-database-ios-app-tutorial/

manglano
  • 844
  • 1
  • 7
  • 21
1

I think as long as there is enough space on your device you can store all of the data you wants... The device is the limit.

NSUserDefaults values are stored into a .plist file:

See this post to know where: Get NSUserDefaults plist file from device

Community
  • 1
  • 1
Thomas
  • 1,289
  • 1
  • 17
  • 29
  • I'm getting this error: ```Attempting to store >= 4194304 bytes of data in CFPreferences/NSUserDefaults on this platform is invalid. This is a bug in MyApp or a library it uses``` – Arjun Feb 11 '22 at 09:01
  • See the answer below, it says: `the limit is the maximum file size for iOS (~4GB)` also check out the sources – Thomas Feb 11 '22 at 13:04