cloudkitcksubscription

Cloud kit: how to get public database subscription notification from your group of contacts only


I have CKRecord for Location details in the public database. I want to create a subscription for a CKrecord with location details, stored in the public database. I want to set subscription so I get notification from those that are in my contacts group only. The subscription notification on Location CKrecord notifies me on all users which takes more time to sort through and I want to avoid this.

How can I set the subscription?


Solution

  • Assuming your Location recordType has some kind of userEmail field on it, you can query for those records like this:

    let predicate = NSPredicate(format: "userEmail = %@", argumentArray: ["name@example.com"])
      
    let query = CKQuery(recordType: "Location", predicate: predicate)
    let operation = CKQueryOperation(query: query)
      
    var records = [CKRecord]()
    operation.recordFetchedBlock = { record in
      records.append(record)
    }
    
    operation.queryCompletionBlock = { cursor, error in
      print("Done.")
      print(records)
    }
    
    CKContainer(identifier: "iCloud.yourContainer").publicCloudDatabase.add(operation)