CloudKit references — is this a forward reference or a back reference?

I'm trying to understand the terminology around forward vs backward references in CloudKit.

Say I have two record types:

  • User
  • LeaderboardScore (a score belongs to a user)

The score record stores a user reference:

score["user"] = CKRecord.Reference(
    recordID: userRecordID,
    action: .deleteSelf
)

So:

  • LeaderboardScore → User
  • The user record does not store any references to scores

From a data-model perspective:

  • Is this considered a forward reference (child → parent)?
  • Or a back reference, since the score is "pointing back" to its owner?

My use case is having leaderboard in my app and so i have created a user table to store all the users and a score table for saving the scores of each user of the app.

Answered by DTS Engineer in 878074022

In CloudKit, a reference (CKRecord.Reference) is a forward reference, that stores a field pointing to another record’s ID. In your case, LeaderboardScore.user is a field points to a user (User), and the user doesn't have anything pointing back to the leaderboard score.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

In CloudKit, a reference (CKRecord.Reference) is a forward reference, that stores a field pointing to another record’s ID. In your case, LeaderboardScore.user is a field points to a user (User), and the user doesn't have anything pointing back to the leaderboard score.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thanks for the reply Ziqiao.

Currently, LeaderboardScore stores a reference to User, and User does not store an array of scores. My understanding was that this follows the WWDC guidance (like the shoebox example), where child records reference the parent rather than the parent embedding children.

When you mentioned that nothing in User points back to the scores, are you suggesting I should explicitly store score references in the User record? Wouldn’t that effectively mean storing scores in the user table, which doesn’t seem conceptually right?

Just trying to confirm whether my current forward-reference model is already the intended approach, or if I’m misunderstanding what “back references” mean in this context.

Accepted Answer

No, I was not suggesting that you store score references in the User record. Instead, I was just trying to explain how CKRecord.Reference works.

What you have for now looks good to me, and is the right way to use CKRecord.Reference.

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

Thank you so much for the quick response. Looks like I can move forward in my work now. I was just hung on whether i am creating a bad schema but this clears up the confusion so thank you.

CloudKit references — is this a forward reference or a back reference?
 
 
Q