cloudkit set record id

How do I set the record id in cloudkit to prevent duplicate records. I am using swift to develop an app.

Replies

You can use the default recordId in intWithRecordType: or roll your own with

https://developer.apple.com/documentation/foundation/nsuuid?language=objc

I am confused as to how this works in swift.


Here is what I am using to generate cloudkit records.


let record = CKRecord(recordType: RemoteRecord.azmet)

record[RemoteAzmet.dayOfYear] = Int(dayOfYear) as CKRecordValue?


Cotton.share.publicDB.save(record)


How do I modify this to generate unique record id's


Thanke


Russ

I think your question has a simple answer but I have avoided learning Swift. Sorry.

Hi Russ!


There are a couple ways to do this.

First, it does not seem that there is a way to tell CloudKit directly that your record type can only contain unique records.

It also does not seem that there is a way to tell Cloudkit that any particular key/value must have only one record with a particular value.

It appears that the only parameter in a CloudKit record which must be unique is the RecordID.

Thus, you have a few different options to force the RecordType to only store records which are unique:


1) You can initialize your record with your own RecordID that you choose:

init(__recordType recordType: String, recordID: CKRecord.ID)

In this situation, you take the "value" of the "key-value" pair which must be unique and you hash it to a unique has that is 255 or fewer characters. Use this has as your recordID.


2) You can set up a predicate that uses the key-value pairs which you say must be unique. You then first search the if it extracts a record or not. If not, then quickly save your new record. If a record exists, then you have three choices, depending on the design of your application (1: fail to save the new record; 2) modify the existing record with your new record, using the retrieved recordID; 3) modify your new record to be unique and start this assertion over). If you put all this into an operation block, it will reduce or eliminate race issues.


If you find that there is indeed a different way to force unique records, please do let me know. In SQL type language, the keyword would be called DISTINCT. I have not been able to find such a counterpart in cloudkit.


Hope this helps.


CharlesK

first computer program 1977

I want to initilize my records with a unique RecordID. But I cant seem to find the place in my code to insert the init.


enum RemoteRecord {

static let azmet = "Azmet

}


enum RemoteAzmet {

static let dayOfYear = "dayOfYear"

static let stress = "stress"

static let stationDoyYear = "stationDoyYear"

}

class Cotton {


static let share = Cotton()


var container: CKContainer

var publicDB: CKDatabase

var privateDB: CKDatabase

var sharedDB: CKDatabase


private init() {

container = CKContainer.default()

publicDB = container.publicCloudDatabase

privateDB = container.privateCloudDatabase

sharedDB = container.sharedCloudDatabase

}

}

enum RemoteRecord {

static let azmet = "Azmet"

}


class AddAzmet {


func addToCloud (stationDoyYear:String,stress:Double) {


// let recordIDString = CKRecordID(recordName: stationDoyYear) This is what I want to be recordID

let record = CKRecord(recordType: RemoteRecord.azmet)

record[RemoteAzmet.stress] = stress as CKRecordValue?

Cotton.share.publicDB.save(record) {

record, error in

if error != nil {

print(error!.localizedDescription)

} else {

print("\nrecords uploaded \(String(describing: record))")

}

}

return

}

}

This works to add recorde to cloud kit, but does allow duplicate records with the same stationDoyYear which is the name of a weather station + the day of the year + the year, which is unique.


This should be simple but I have not been able to get anything to work. I keep gettin an Initializers may only be declared within a type. error.


Thanks for your help