Sorry everyone for garbaging this thread a bit, but I figured out one thorny issue with this whole thing. It's regarding the situation when an owner stops sharing a record.
So, to recap, Fat Xu discovered using purgeObjectsAndRecordsInZone (after making a deep copy) does work to keep the owner's database correct. I was having the issue that the other participants were not getting notification of this change. That was why I went down the road of using viewContext.delete, and then later cleaning up the empty zones so that the change would be received by recipients and the owner wouldn't have a bunch of empty zones building up over time.
I finally discovered why my participants were not getting the notification. It was a bug in Apple's sample app for this. (10015: Build Apps that Share Data Through CloudKit and Core Data). I had used this sample app as a basis for processing remote store notifications.
In that code, after fetching the NSPersistentHistoryTransactions, it checks to see if the results are NSPersistentHistoryResult AND is not empty. That last bit was the problem. I tried searching for documentation that would explain the NSPersistentHistoryResults more, but unfortunately, Apple did not give us much to go by. It turns out that when it's processing the scenario where the owner of a share purges the zone, the result looks different than if it were other types of transactions. The NSPersistentHistoryResult.result is empty, so my code (again, based on the sample code) ignored it.
Once I removed that check for transactions being empty, it correctly passed the notification to my UI that the record is no longer shared and should be deleted.
So, disregard my previous answer with the code to delete unused zones. Just use FatXu's method of making a deep copy, then purging the zone, and as long as you're processing the notifications and persistent history correctly (not using Apple's sample code), then it will work for you.
Cheers!
Post
Replies
Boosts
Views
Activity
For the empty zone cleanup I did the following.
(For context see my post above. It wouldn't let me paste code blocks in my reply otherwise)
In the init of my CoreDataStack, I added this:
// Perform async function to clean up unused share zones leftover from deletions
Task {
do {
try await performZoneCleanup()
} catch {
print("*** Error in performZoneCleanup: ", error)
}
}
And then added this function:
func performZoneCleanup() async throws {
// Get list of shared zones
let container = CKContainer.default()
let privateDB = container.privateCloudDatabase
let zones = try await privateDB.allRecordZones()
// Filter to keep empty ones
for zone in zones {
let query = CKQuery(recordType: "CD_StudentSubjectMO", predicate: NSPredicate(value: true)) // Adjust record type for your model
let records = try await privateDB.records(matching: query, inZoneWith: zone.zoneID, desiredKeys: nil, resultsLimit: 100)
if records.matchResults.isEmpty && zone.zoneID.zoneName != CKRecordZone.ID.defaultZoneName {
let zoneID = try await persistentContainer.purgeObjectsAndRecordsInZone(with: zone.zoneID, in: privatePersistentStore)
print("ZoneID purged: \(zoneID)")
}
}
print("CoreDataStack has completed empty zone cleanup")
}
Note: You may get some console warnings from NSCloudKitMirroringDelegate saying it can't find the zones you purged, especially on other devices, but I don't see a way around that until Apple cleans this process up for us.
I've discovered a workaround to the issue of the local cache not being updated when the owner stops sharing.
At first, I tried Fat Xu's method of first making a copy of the record and then using purgeObjectsAndRecordsInZone (essentially resetting things to the way they were before sharing.) The downside to this method is that the participants are not notified of the change, as Fat Xu pointed out.
However,
If, instead of using purgeObjectsAndRecordsInZone, you use a simple context.delete() function to delete the record (after making a copy), then the participants do get notified. There must be something about purging the zones that screws up the participant's effort to maintain sync.
The only remaining issue is that you'll have empty share zones in your container.
I chose to solve this by running a background task upon startup to delete all empty zones that were set up for sharing.
It's working for me so far, so I figured I'd let you guys know about it.
Glad you pointed this out. This is a huge oversight. I hadn't noticed it until you said something and was about to ship an app. Guess I'll have to wait a little longer in the hopes that Apple fixes it. If you wouldn't mind posting if/when you hear back from Apple on this, I'd appreciate it.
I guess a short term solution could be for people to select "Share Options" and use the "Copy Link" button to be able to add people?