I'm trying to create a tagging system using CloudKit. I have a type called Article and an tag type called Tag. Each article can have multiple tags applied. I want my data to be normalied so since same tags can appear on several articles and articles can have multiple tags, I need a many-to-many relation. In old school DB-stuff this would require a junction table.
How do you do that in CloudKit?
In any of Apples documentation I can only find examples of one-to-many relations.
So I have created a junction-table type called ArticleTag, that consists og two CKReferences. One for the Article reference and one for the Tag reference.
This should work, but what is the best way to query for tags per article?
Kind regards, Esben
There isn't currently any mechanism to do "join" queries in CloudKit like a traditional relational database, however you should be able to accomplish what you want by using a field with the type "Reference List" on your Articles. Let's assume the following:
You can then lookup an Article's tags using the IDs in the tags field, and you can find all Articles tagged with a specific tag by using the Tag record ID and a CKQuery predicate that uses the CONTAINS operation to check for list membership.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With