[User Defaults] CFPrefsPlistSource Bug

Hi, I'm trying to save in the UserDefaults some identifiers for user-edited videos that we save on the Defaults. We save in this way:

let data = try JSONEncoder().encode(myDesign)
Defaults.setValue(data, forKey: myDesign.id)
[...]
allMyDesignsIds.append(myDesign.id)
Defaults[.allUsersMyDesignsIds][userId] = allMyDesignsIds

but we got an error when saving 10 elements, in the saving of the 8 element :

2023-08-01 17:44:08.656097+0200 AppName[12474:355884] [User Defaults] CFPrefsPlistSource<0x280d34480> (Domain: com.***.***.beta, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: Yes): Attempting to store >= 4194304 bytes of data in CFPreferences/NSUserDefaults on this platform is invalid. This is a bug in AppName or a library it uses.
Description of keys being set:
985D7A7E-A0DC-4F26-BB1A-7202B106F49F: data value, size: 584750

Description of keys already present:
9E13F8B4-789F-4BA8-B788-6AAA924668D2: data value, size: 643913
63AC4E4F-7DA4-441D-9B77-5728689E6E74: data value, size: 642935
B0038C53-F4F6-46B7-996B-375F95E83FEA: data value, size: 637857
FDBC800B-89CE-4D2C-A89F-E96542A8EF93: data value, size: 611401
985D7A7E-A0DC-4F26-BB1A-7202B106F49F: data value, size: 584750
6D0524A1-E17D-4B96-B928-A9180D47107B: data value, size: 555791
DEBF1AF1-866A-42FD-8603-23DD1051E5FD: data value, size: 279035
87D83169-89B1-4A58-A63D-13CF118A5202: data value, size: 250166
35C48378-0CD3-457D-8BEB-DA41D855C3EE: data value, size: 226120
com.facebook.sdk:serverConfiguration631812514659191: data value, size: 3228
...
Total keys: 95 - Average approximate value size: 46758 bytes

2023-08-01 17:44:08.656513+0200 AppName[12474:355884] [User Defaults] CFPrefsPlistSource<0x280d34480> (Domain: com.***.***.beta, User: kCFPreferencesCurrentUser, ByHost: No, Container: (null), Contents Need Refresh: No): Transitioning into direct mode

In this case the app cleans the values of Defaults[.allUsersMyDesignsIds][userId] leaving it empty...

I tried it with the same videos always and also it only happens in the first launch and the first savings of videos in the Defaults...

Anyone knows what can be happening? We discard the size of the values, because after this bug we continues saving videos without any problem.

Thanks!!

It looks like you're trying to save 10 data blobs whose average size is about 400K, which is a total of about 4MB, which is what the error message says is the limit. In general, you shouldn't expect to store that much data in UserDefaults.

Perhaps you can consider storing the data itself somewhere else in the file system, and just store the keys in UserDefaults?

You said:

Im saving 1 data blob and updating another element with the id of the saved data, and I do it in X times

In that case, I it'd be helpful if you could post a bit more code, to clarify what's going on. In your post, I see:

Defaults.setValue(data, forKey: myDesign.id)
...
allMyDesignsIds.append(myDesign.id)
Defaults[.allUsersMyDesignsIds][userId] = allMyDesignsIds

That sure looks like you're storing one data blob under each key, then storing the array of keys. It also looks like you you have multiple keys, therefore multiple data blobs. That may not be what's going on here, but there's not enough information to understand it.

[User Defaults] CFPrefsPlistSource Bug
 
 
Q