试图快速修改ckrecord
Posted
技术标签:
【中文标题】试图快速修改ckrecord【英文标题】:Trying to modify ckrecord in swift 【发布时间】:2016-09-05 18:42:31 【问题描述】:我希望在 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!
【问题讨论】:
什么是locationRecord
?
让 locationRecord = CKRecord(recordType: "location") @matt
好吧,这就是你的问题。如果您想修改现有记录,为什么要创建新记录?
我将如何修改它而不是@matt
【参考方案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)
)
【讨论】:
以上是关于试图快速修改ckrecord的主要内容,如果未能解决你的问题,请参考以下文章
修改 CKRecords 时出现 CloudKit 错误:“Zone Busy”