尝试修改 swift 中的 ckrecord
Trying to modify ckrecord in swift
我希望在 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!
}
}
要修改记录,您需要拉入原始记录 - 更新详细信息,然后保存记录。
我会通过缓存对原始 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)
}
})
我希望在 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!
}
}
要修改记录,您需要拉入原始记录 - 更新详细信息,然后保存记录。
我会通过缓存对原始 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)
}
})