我希望在cloudkit中更新用户的位置,但我的代码每次都会保存一条新记录。现有记录如何被新的记录修改或替换?
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
locationRecord.setObject(location, forKey: "location")
let publicData = CKContainer.defaultContainer().publicCloudDatabase
publicData.saveRecord(locationRecord) { record, error in
}
if error == nil
{
print("Location saved")
self.loc1 = location!
}
}
答案 0 :(得分:1)
要修改记录,您需要提取您的原始记录 - 更新详细信息,然后保存记录。
我会通过缓存对原始recordID的引用来实现此目的,例如
var locationRecordforUser = (whatever your CKrecord was when you created it)
然后在上面的位置方法中,只需抓住它,进行更改并执行保存
record.setValue(locationbits, forKey: locationField)
self. publicData.saveRecord(record, completionHandler: { (savedRecord, error) in
dispatch_async(dispatch_get_main_queue()) {
completion(savedRecord, error)
}
})