Attempting to store bytes of data in CFPreferences/NSUserDefaults on this platform is invalid

I am not really sure what is the cause but this happens if i overwrite values. if i clear the app from scratch and store them all it works ok. Thoughts?

my exact message is Attempting to store >= 4194304 bytes of data in CFPreferences/NSUserDefaults on this platform is invalid

Description of keys being set:
holocene_update: string value, approximate encoded size: 25

Description of keys already present:
holocene: string value, approximate encoded size: 5216090
GMSMapsUserClientZwiebackCookie: string value, approximate encoded size: 175
GMSMapsUserCookie: data value, size: 48
kGMSServerVersionMetadataTrackerSavedServerVersionMetadataKey: data value, size: 30
holocene_update: string value, approximate encoded size: 25
com.google.Maps.GMSCoreLastVersion: string value, approximate encoded size: 6
cyclone_alert_last: string value, approximate encoded size: 5
com.google.Maps.GMSSDKLastVersion: string value, approximate encoded size: 5
GMSMapsUserClientCohort: string value, approximate encoded size: 4
kGMSMapsUserClientLegalCountry: string value, approximate encoded size: 2
...
Total keys: 12 - Average approximate value size: 434699 bytes
2022-12-19 17:49:08.777899+0800 PH Weather And Earthquake Updates[31305:4102377] [User Defaults] CFPrefsPlistSource<0x600002b80980> (Domain: ************, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: No): Transitioning into direct mode

Is there a way to increase this limit? I need to store data.

Answered by DTS Engineer in 740194022

UserDefaults isn't intended to store such large amounts of data. It would be more usual to persist data like this in a file in a known location.

Accepted Answer

UserDefaults isn't intended to store such large amounts of data. It would be more usual to persist data like this in a file in a known location.

This limit exists since a long time, as shown in this old post: https://developer.apple.com/forums/thread/121527

Are you trying to store an image ?

You should store in a file and store the file URL in UserDefaults.

The same for any type of data.

Note: 4 194 304 is exactly 2 ** 22.

Attempting to store bytes of data in CFPreferences/NSUserDefaults on this platform is invalid
 
 
Q