0

I'm trying to make an app which stores a user's comment on CloudKit and then shows it to the other users. User simply enters his/her comment on a text field and clicks on a submit button to submit his/her comment (just like a restaurant app). However, I can't seem to find the correct way no matter what I try. Here is my code, I'd be very glad for any help as I've been stuck on this problem for some time now. Thank you very much in advance!

@IBAction func OnSubmitTouched(_ sender: UIButton) {
    if (textField.text != ""){

        let newComment = CKRecord(recordType: "Users")
        let publicDB = CKContainer.default().publicCloudDatabase
        newComment.setValue(textField.text!, forKey: "comment")
        publicDB.save(newComment){
            rec ,err in
            if let error = err {

            print(err.debugDescription)
            return
            }

            publicDB.fetch(withRecordID: newComment.recordID){
                rec, err in

                print(rec!["comment"]!)
                return
            }
        }
        let predicate = NSPredicate(value: true)
        let query = CKQuery(recordType: "comment", predicate: predicate)
        let operation = CKQueryOperation(query: query)

        var commentRecords: [CKRecord] = []

        operation.recordFetchedBlock = { record in
            commentRecords.append(record)
        }

        operation.queryCompletionBlock = { cursor, error in
            print(commentRecords)
        }

        CKContainer.default().publicCloudDatabase.add(operation)
   }
}

1 Answers1

1

You are getting a permission error because Users is a protected record type that CloudKit creates automatically for users of your app. You should name it something else and then it should work.

For example, you could make a Comment record type. This might need a field that references the current user. You can get the current userID with:

CKContainer fetchUserRecordIDWithCompletionHandler:

Here is the Apple documentation for this method.

It is also possible to use the Users record type, but you would have to find the existing userID from CloudKit as above then build a record around that.

See also this answer.

Chris
  • 4,009
  • 3
  • 21
  • 52
  • Thanks a lot for your answer!! I don't get any errors now during the debugging process and XCode successfully prints the comment I entered. However, nothing seems to appear on my dashboard in my newly created record. How can I fix this issue?? Thanks again in advance. – Ege Berk Akkaya Aug 19 '18 at 11:50
  • Are you querying for your new record type in the dashboard rather than users? – Chris Aug 19 '18 at 11:52
  • Both Users and NewUsers (my other record type) give me this error in the dashboard: There was a problem querying the “Users” (or "NewUsers") type. Field 'recordName' is not marked queryable. Enable the “queryable” index on this field in the indexes section. However I double checked and everything is already set as queryable, searchable and sortable. – Ege Berk Akkaya Aug 19 '18 at 12:28
  • Hmm I’m not sure. I’ll have a look when I’m back at my computer. – Chris Aug 19 '18 at 18:30
  • Thank you very much for spending your time – Ege Berk Akkaya Aug 19 '18 at 23:08
  • @EgeBerkAkkaya I’m afraid I cannot find an answer, other than making sure everything is set ok in dashboard. [This question on Apple forums](https://forums.developer.apple.com/thread/79126) seemed promising but the only answer may not be useful. – Chris Aug 20 '18 at 19:32