swift - How to add fields to existing records in CloudKit -
i have function input users name new recordname in cloudkit (shown below), need add users 'score' same record.
is there way insert field "usersscore" referencing unique 'recordname' value?
/// insert new entry db. func insertname(nameentry: string) -> ckrecordid { // connects user database table. let usertable = ckrecord(recordtype: "user") // connects database public container. let publicdata = ckcontainer.default().publicclouddatabase // adds users entry "name" field. usertable["name"] = nameentry nsstring // if record has saved or error has occurred. publicdata.save(usertable) { (record, error) in if let saveerror = error { //error. print("an error occurred in \(saveerror)") }else { // success. print("saved record!") } } // recordname (id) of current user. recordnameid = usertable.recordid return recordnameid! }
to enter multiple records it's better useckmodifyrecordsoperation
. here's how did it...
func insertitems(nameentry: string) { // creates record name based on name passed function. let usernamesid = ckrecordid(recordname: "\(nameentry)") // inserts name user table. let username = ckrecord.init(recordtype: "user", recordid: usernamesid) // adds users entry "name" field along score , amount of questions answered. username["name"] = nameentry ckrecordvalue username["score"] = questionscorr ckrecordvalue username["questions_answered"] = questionsans ckrecordvalue // insert records database. publicdatabase.add(ckmodifyrecordsoperation.init(recordstosave: [username], recordidstodelete: nil)) }
wiki
Comments
Post a Comment