CloudKit Not Saving Records

Hi ..

Im working on CloudKit, I made new iOS project and added a CloudKit Capability, as well as a container, imported it in a View Controller and tried to save data as code below, but nothing is happeing and no data is being saved when I go to the dashboard ?


@IBAction func addReport(_ sender: Any) {
        
        let bugReport = CKRecord(recordType: "NewReport")
        bugReport ["BugName"] = "Bug 1"
        bugReport ["BugDisc"] = "Its a main bug"
        
        let container = CKContainer.default()
        let database = container.publicCloudDatabase
        database.save(bugReport) {(savedRecords, error) in
            
        }
        
    }

I looked at this tutorial.

h ttps://www.hackingwithswift.com/read/33/7/working-with-cloudkit-records-ckrecordreference-fetchwithrecordid-and-save


Some differences:

they cast to

CKRecordValue
bugReport ["BugName"] = "Bug 1" as CKRecordValue


You don't show code in save, maybe there is an issue here.

Here is an example:

CKContainer.default().publicCloudDatabase.save(whistleRecord) { [unowned self] record, error in
    DispatchQueue.main.async {
        if error == nil {
            self.suggestions.append(suggestion)
            self.tableView.reloadData()
        } else {
            let ac = UIAlertController(title: "Error", message: "There was a problem submitting your suggestion: \(error!.localizedDescription)", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            self.present(ac, animated: true)
        }
    }
}


Hope that helps.

Hi


Thanks for the update, my code is based on a tutorial in pluralsight and they cast to NSString not CKRecordValue, but I tried CKRecordValue as well and didnt work. I used you code for the clouser I get the error message "couldnt get container configuration from the server" although in app settings its configured properly ?

Can you show the code where you get the error ?

its the code I posted the first time ? didnt get what you mean ?

Below is link to error the App shows after using your error catch code ..


https://www.dropbox.com/s/5tcs8w18rbaado9/8DF4D2DF-5905-4D50-B3B7-8CB6220E7E17.jpeg?dl=0

I replied with a link to image that shows the error dont know if you see it or modertator still pending it ?

Link does not work (error 404, unfound)

Hi


When I added your code I got a run time error the project is in link below if you can kindly take a look.

--

Kindest Regards


h ttps://www.dropbox.com/s/2wzc91tqwdfzoxk/CloudKitDemo%20copy.zip?dl=0

Is the error when you tap on Button ?


UI functions must occur on main thread.

So replace with following (I added the print to see what's happening).


    @IBAction func addReport(_ sender: Any) {
       
        var bugReport = CKRecord(recordType: "BugReport")
        bugReport ["BugName"] = "Bug 1" as NSString
        bugReport ["BugDisc"] = "Its a main bug" as NSString
       
        let container = CKContainer.default()
        let database = container.publicCloudDatabase
        database.save(bugReport) {record, error in
            if error == nil {
                print("OK")
            } else {
                print("NOK")
                DispatchQueue.main.async { () -> Void in
                    let ac = UIAlertController(title: "Error", message: "There was a problem submitting your suggestion: \(error!.localizedDescription)", preferredStyle: .alert)
                    ac.addAction(UIAlertAction(title: "OK", style: .default))
                    self.present(ac, animated: true)
                }
            }
           
        }
    }

Hi


Thank you very much for the replay and code, now I get the error message its cought by the code you made, at least

it give an indea but dont know why it happens everything seems fine !, please check last image in link below ..


https://www.icloud.com/sharedalbum/#B0zJFYC51GkvQ0E

What is iCloud.vision.honest.cloud-kitdemo ?


I note your bundle is

iCloud.vision.honest.cloudkitdemo

without dash


Is there a mismatch here ?

Its the default container I have a screen of the App iCloud settings

Should check that the container is valid.

The message tells you it is not…

Its valed and as I think it shows automatically in the App Settins so I just selected it

Aha sorry theres something strange here althought I choosen the correct container its trying to write to the App bundle identifier name !

CloudKit Not Saving Records
 
 
Q