title: Saving An Array Of Objects In CloudKit
body: <p>I'm confused about how to best save an object that contains an array of other objects in CloudKit.</p>
  
  <p>Say I have a todo list app, which has different collections of items. How would I go about saving/fetching a whole collection of items?</p>
  
  <p>Would I have a Record type of Collection, which would have a String Attribute called "Name", and then a Reference List Attribute called "Items"?</p>
  
  <p>I know that the Record type of Item needs to have a Reference Attribute called "Collection", because of how CloudKit references from a child object to its parent.</p>
  
  <p>I have managed to save a Collection without any instances of Item with the following code</p>
  
  <pre><code>func addCollection(collection: Collection!, completion: (error: NSError!) -&gt; ()) {
      if collection == nil
          return
      }
      let collectionRecord = CKRecord(recordType: "Collection")
      CollectionRecord.setObject(collection.name, forKey: "Name")
      privateDB.saveRecord(collectionRecord) {
          record, error in
          dispatch_async(dispatch_get_main_queue()) {
             completion(error: error)
          }
      }
  }
  </code></pre>
  
  <p>The other option is when saving a Collection, to loop through all instances of Item and also save those individually, their Reference Attribute to Collection making the connection on the CloudKit side, but this seems like way too many network calls.</p>