Best practice for tracking whether records belong in shared database?

If I need to write a child record of a shared root record, what's the best way to determine whether I write it to the private or shared database?

For shared root records, record.share is non-nil. But for child records of that root record, record.share is nil.

I understand that child records have a parent reference that can be resolved to determine whether the parent has a share, but resolving that reference is increasingly expensive the more deeply the child is nested.

Given an arbitrarily deeply nested child record, is there a cheap and canonical way to determine whether the record should be written to the shared database?

Replies

You can find the information you are looking using the CKRecordZone.ID and a little bit of logic! Since the Zone ID contains the owner name, you can just compare that to the active user (CKCurrentUserDefaultName) to see if we are writing into our own database or someone else’s (since users can’t create zones in other user’s databases, that means if we don’t own it we are targeting the sharedDB).

I generally recommend to treat the CKRecordZone.ID as just as critical information alongside of the CKRecord’s metadata, even if you aren’t using multiple databases, since without it you lose the ability to accurately target records if you are to expand into multiple zones/databases in the future — I go as far as tombstoning this information along with the CKRecord.ID after local deletion since it can become a nightmare to deal with offline/failed deletes without it.

  • Your answer made my day! I don't know how I missed that, especially since, as you suggest, I'm already tombstoning the CKRecordZone.ID in my local cache for deferred deletes.

    You wouldn't believe the circuitous solutions I've tried, the last of which worked (traversing the parent chain upwards to the root). This is wonderfully simple.

    In short, you rock, JimmyCricket—thanks!

Add a Comment