My app uses CoreData with iCloud as backend. Multiple devices can access the iCloud database which is thus .public
.
The local CoreData store is synchronized with iCloud using an NSPersistentCloudKitContainer
.
I use history tracking according to Apple’s suggestions.
There, Apple suggests to prune history when possible. They say
Because persistent history tracking transactions take up space on disk, determine a clean-up strategy to remove them when they are no longer needed. Before pruning history, a single gatekeeper should ensure that your app and its clients have consumed the history they need.
Originally this was also suggested in the WWDC 2017 talk starting at 26:10.
My question is: How do I implement this single gatekeeper?
I assume the idea is that a single instance knows at what time every user of the app has last synchronized their device. If so the history of transactions before this date can be pruned.
But what if a user synchronized the local data and then does no longer use the app for a long time? In this instance the history cannot be pruned until this user again synchronizes the local data. So the history data could grow arbitrarily large. This seems to me as a central problem that I don’t know how to solve.
The Apple docs cited above suggest:
Similar to fetching history, you can use deleteHistory(before:) to delete history older than a token, a transaction, or a date. For example, you can delete all transactions older than seven days.
But this does not solve the problem to my mind.
Aside of this general problem, my idea is to have an iCloud record type in the public iCloud database that stores for every device directly (i.e. without CoreData) the last date when the local database was updated. Since all devices can read these records it is easy to identify the last time when all local databases have been updated and I could prune the history before this date.
Is this the right way to handle the problem?
EDIT:
The problem has recently been addressed in this post. The author demonstrates with tests with Apple's demo app that there is indeed a problem, if the history is purged too early. My answer there indicates that with the suggested delay of 7 days, an error is probably extremely rare.
NSPersistentStoreRemoteChange
will be more than 7 days. I am able simulate the problem, by changing 7 days to 2 minutes for experimenting purpose - #72557560 Perhaps I am doing testing the wrong way? – Parr