Storing data in CloudKit

To store objects in CloudKit, you use CKModifyRecordsOperation. This operation takes a list of records that should be created, updated, or removed. Every record is connected to a certain zone, should have a unique identifier, and can have different properties associated with it. If you add a property to an object that doesn't exist in CloudKit yet, CloudKit will automatically update the data model on the server using the data you sent. You can even create entirely new models this way. You can add the following code to CloudStore.swift to store a very basic version of a family member's record:

extension CloudStore {
  func storeFamilyMember(_ familyMember: FamilyMember, _ completionHandler: @escaping (Error?) -> Void) {
    let defaultZoneId = CKRecordZone.ID(zoneName: "moviesZone", ownerName: CKCurrentUserDefaultName)
    let recordId = CKRecord.ID(recordName: UUID().uuidString, zoneID: defaultZoneId)
    let record = CKRecord(recordType: "FamilyMember", recordID: recordId)
    record["name"] = familyMember.name!

    let operation = CKModifyRecordsOperation(recordsToSave: [record], recordIDsToDelete: [])
    operation.modifyRecordsCompletionBlock = { records, recordIds, error in
      guard let records = records, error == nil else {
        completionHandler(error)
        return
      }

      print(records)
    }

    privateDatabase.add(operation)
  }
}

The preceding code creates a zone ID and a record ID that will be used to identify the record in the database. Next, a family member record is created and the family member name is added to it. Lastly, CKModifyRecordsOperation is created and added to the database to send the record to CloudKit. To call this method when a new family member is added, update the saveFamilyMember(withName:) method in FamilyMembersViewController as follows:

func saveFamilyMember(withName name: String) {
  let moc = persistentContainer.viewContext

  moc.persist {
    let familyMember = FamilyMember(context: moc)
    familyMember.name = name
    familyMember.uuidString = UUID().uuidString

    self.cloudStore.storeFamilyMember(familyMember) { _ in
      // no action
    }
  }
}

This snippet passes the family member to the cloud store so it can convert it to CKRecord and store it in CloudKit. Try adding a new family member in the app, you should see your newly-inserted record in the console in Xcode; when you refresh the list of family members in the CloudKit dashboard, a new entry should have appeared.

This implementation of adding a family member to CloudKit is too basic because there is no way to associate one of the local family members to the family members stored in CloudKit. The next section will implement a proper local cache of the data in CloudKit by adapting the existing Core Data model a little bit so you can import data from the CloudKit server, send new family members and movies to CloudKit, and update existing family members when they add new movies to their favorites.

Since your app stores database change tokens in UserDefaults, you should delete MustC from your device to remove the existing change tokens. This allows you to start fresh and receive all data again.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.147.190