luke luke - 7 months ago 64
Swift Question

How do I create this data/model on Cloudkit to display it in my app?

Fairly new to Cloudkit and I've been figuring out how I can model my data. Below I have given a small sample of code that similar to my model.

struct Street {
var house: [Home]
}

struct Home {
var people: [Person]
var houseNumber: Int
}
struct Person {
var firstName: String
var lastName: String
var age: Int
}


I would want to store this data on the Cloud then fetch the data to be displayed on my app. Thing is I'm not sure how to go about storing the data.

How would I store it so all the data is linked together?

If I had a record of Street which had a field of name, how would i be able to link the name to the age of the person? Would I need to store 3 different records? If so, how would i link the records together?

I assume you have to use references but I'm not sure how to go about it.

I hope i'm explaining it clear to what I'm trying to do.

Answer

For starters, please check out Apple's CloudKit Quick Start documentation. It will go over everything I am about to say in much greater detail and I highly recommend reading it.

Much like the structs you have modeled in your code, you will create similar objects in a web portal called CloudKit Dashboard. This web portal will become available to you after the iCloud entitlements have properly been added to your app ID.

In the sidebar, you will see "Record Types". This is where you may go to create your objects without needing any code. The Quick Start Guide gives details on how to go about creating an object, also known as a "record type". (An instance of that object would be called a record.) Basically, you will need to create fields that correspond to the primitive properties of the object, but in general don't edit the unique identifier placed in the record's name field. For example, your Person record type might have fields firstName (of type String), lastName (of type String), and age (of type Int).

Your Home record type, then, might have a houseNumber field, of type Int.

The way CloudKit handles relationships is a bit different than local storage solutions such as Core Data. For efficiency reasons, we don't really want objects to hold a group of associated objects, since fetching a Street, say, would then need to fetch each House ID, and then fetch each Person ID in the House.

So instead of placing an array of House objects on Street, we place a field of type Reference (to a House) on our Street record type.

So with this new model of record types you would set up in CloudKit Dashboard, you could then in code create CKRecord objects and setting properties corresponding to the fields. Telling a CKRecord representing a House which people live in it would be done by setting the house property on each of the CKRecords representing the people in the house.

You could then fetch a house using a CKQuery, for example, and then fetch it's people by creating a CKQuery for all "People" objects that have a record ID corresponding to the house object in their house reference field.

See Adding Reference Fields in the CloudKit Quick Start Guide.

Edit: When setting the value of a Reference field in CloudKit Dashboard, the value would be the unique ID name of the record you are referencing.