Search code examples
schemacloudkitckrecord

CloudKit schema design


I have written my local data model using Core Data. It is object oriented, so I have something like this in my Core Data model:

class Pair {
   var uuid: NSUUID
   var me: Person
   var friend: Person
}

class Person {
   var name: String
   var birthday: NSDate
   var address: Address
}

class Address {
   var streetNumber: Int16
   var streetName: String
   var zip: Int16
}

Now in CloudKit, I am realizing if I want to get a "Pair" and all of its data, and I make "Person" and "Address" CKReference then I will have to fetch "Person" and "Address" which is two extra network calls and lots of logic to handle their results asynchronously. So I was thinking of "denormalizing" this data and putting it all into a "Pair" CKRecord in the CloudKit schema, without any CKReferences. So more along the lines of this:

class CloudKitPair {
   var uuid: NSUUID
   var myName: String
   var myBirthday: NSDate
   var myStreetNumber: Int16
   var myStreetName: String
   var myZip: Int16
   var friendName: String
   var friendBirthday: NSDate
   var friendStreetNumber: Int16
   var friendStreetName: String
   var friendZip: Int16
}

Is it legitimate to design my CloudKit schema this way to avoid extra network fetch calls for CKReferences?


Solution

  • Is it legitimate to design my CloudKit schema this way to avoid extra network fetch calls for CKReferences?

    I would say the answer is yes, it is a legitimate design.

    Note that the tradeoff you are making is prioritizing efficient network calls over efficient use of iCloud database space.

    Your proposed design will cause you to store the same information more than once. Depending on your use cases, this could be many more times than would be ideal:

    e.g.

    • Person A <-pairs with-> Person B
    • Person A <-pairs with-> Person C
    • Person A <-pairs with-> Person D

    If this series goes on much further, then you are storing Person A many times over in the database and it could be considered a great waste of space. On the other hand, it might be completely reasonable for how you intend to use the data.

    I'd suggest modeling your use cases out and measuring them against the usage limits that Apple provides for iCloud.