试图快速修改ckrecord [英] Trying to modify ckrecord in swift

查看:78
本文介绍了试图快速修改ckrecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在cloudkit中更新用户的位置,但是我的代码每次都只保存一个新记录。如何修改现有记录或将其替换为新记录?

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


推荐答案

到修改记录,您需要提取原始记录-更新详细信息,然后保存记录。

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

我可以通过缓存对原始recordID的引用来做到这一点,例如

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

这篇关于试图快速修改ckrecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆