Trying to modify ckrecord in swift

心已入冬 提交于 2020-01-07 08:35:32

问题


I want the users' location to be updated in cloudkit but my code just save a new record every time. How can the existing record be modified or replaced by the new one?

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!
            }
    }

回答1:


To Modify a record, you need to pull in your orignal record - update the details and then save the record.

I would do this by caching a reference to the original recordID eg

var locationRecordforUser = (whatever your CKrecord was when you created it)

then in your location method above, just grab it, make changes and perform save

 record.setValue(locationbits, forKey: locationField)
            self. publicData.saveRecord(record, completionHandler: { (savedRecord, error) in
              dispatch_async(dispatch_get_main_queue()) {
                  completion(savedRecord, error)
               }
           })


来源:https://stackoverflow.com/questions/39336112/trying-to-modify-ckrecord-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!