简体   繁体   中英

How am I storing more than 1024 keys in NSUbiquitousKeyValueStore?

This is more out of curiosity than anything.

The docs clearly state that there should be a hard limit of 1024 on the number of keys we can store with KVS (Key-Value Store) on iOS.

The total amount of space available in your app's key-value store, for a given user, is 1 MB. There is a per-key value size limit of 1 MB, and a maximum of 1024 keys. If you attempt to write data that exceeds these quotas, the write attempt fails and no change is made to your iCloud key-value storage . In this scenario, the system posts the didChangeExternallyNotification notification with a change reason of NSUbiquitousKeyValueStoreQuotaViolationChange.

I created a demo app that populates 1025 (1 over the limit) distinct keys in KVS. Technically, it's 1026 since I also store an array of ids to index each KVS key.

I used a UITableView to display the data from KVS and it all seems to be there after slowly scrolling through every record.

显示带有数据的 tableview 的 ios 屏幕截图

I can see all 1025 entries listed in my demo app if I scroll from top to bottom.

It seems odd to me that I'm able to exceed that 1024 limit.

I'm able to delete the app, re-install it, and see all the data from KVS download as expected. I have the proper entitlements enabled and synchronization seems to work. So it seems the data must be getting backed up to KVS via iCloud from what I can tell.

Using an iCloud account with the simulator and brctl log --wait --shorten , I located a plist file in the simulator directory on my machine that seems to agree that there are more than 1024 keys being stored for the app

(Note: I am writing keys like "1", "2" , etc like so: NSUbiquitousKeyValueStore.default.set(dict, forKey: id) . The changecount , dirtykeys , and values entries in that plist file seem to be something KVS manages itself. I think this plist may be an under-the-hood peek at KVS in the simulator, but I'm not positive).

plist 内容截图

I'm assuming that I'm somehow making a mistake and I'm misinterpreting what I'm seeing, but any insight into how I am seemingly exceeding this limit would be appreciated.

That's not 1000 keys, that's three keys (changecount, dirtykeys, values) with 2 large arrays inside.

Doing it this way, you'll be OK until you hit the 1MB value limit with one of those large arrays. However, doing it this way you won't be able to look up any of your entries without pulling the whole array to the device, and writing the whole array when you change it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM