如何在Swift中覆盖CloudKit现有数据?

时间:2016-04-28 11:13:59

标签: swift cloudkit overwrite

我必须覆盖CloudKit中的现有数据

我曾尝试使用saveRecord,但它并不适合我。

就我而言,我只需要覆盖一个数据。

那我该怎么办?

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
    if locationEnable == true {
    if loaded == true {
        let location = locations.last! as CLLocation

        let location2 = locations.last!
        var a:String?
        if(Data.count != 0 ){ a = Data[0].valueForKey("identity") as? String}
        else {a = Data3!.valueForKey("identity") as? String}
        let id = CKRecordID(recordName: a!)
        let locationRecord = CKRecord(recordType: "Driver", recordID: id)
        locationRecord.setObject(location2, forKey: "X")
        locationRecord.setObject(a!, forKey: "Identity")
        AddRecordToCloud(locationRecord)
    }
    if UIApplication.sharedApplication().applicationState != .Active {
        print("Background Updating")
    }
    }
}
func AddRecordToCloud(record:CKRecord){
        let publicDatabase = CKContainer.defaultContainer().publicCloudDatabase
        publicDatabase.saveRecord(record, completionHandler: { (record:CKRecord?, error:NSError?) -> Void in
            if(error != nil){print("error:\(error)")}
            else {print("saving")}
        })
    }

这是错误:

error:Optional(<CKError 0x7fb858f5ba80: "Server Record Changed" (14/2004); server message = "record to insert already exists";

1 个答案:

答案 0 :(得分:0)

您必须获取记录,然后更改其中的数据并保存相同的记录。