Handle many-to-many relationships in CloudKit

I am trying to create a tag system using CloudKit. I have a type called Article and a type tag. Each article can have several tags. I want my data to be normalized, since the same tags can appear on several articles, and articles can have several tags, I need a many-to-many relationship. In the old DB-stuff school, this would require a join table.

How do you do it in CloudKit?

In any of the apple documentation, you can only find examples of one-to-many relationships.

So, I created a connection table type called ArticleTag, which consists of two CKReferences. One for linking to an article and one for linking to a tag.

This should work, but what is the best way to request tags in each article?

Regards, Esben

+5
source share
1 answer

There is currently no mechanism for creating join requests in CloudKit, as in a traditional relational database, however you should be able to accomplish what you want using a field with the type β€œLink List” in your articles. Assume the following:

  • The post type of your article has a "tag" field of type Reference List. You can install this in CloudKit Dashboard .
  • You have a tag entry type
  • When tagging an article, you first look at (and potentially create) a tag entry for each tag, then add CKReference to the Article.tags list box using each of the tag entry IDs.

You can then search for article tags using identifiers in the tag field, and you can find all articles tagged with a specific tag using the tag entry identifier and CKQuery , which uses the CONTAINS operation to verify membership in the list.

+5
source

All Articles