2016-09-05 31 views
-1

我希望用戶的位置在cloudkit中更新,但我的代碼每次只保存一條新記錄。現有記錄如何修改或替換爲新記錄?試圖在swift中修改ckrecord

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

什麼是'locationRecord '? – matt

+0

let locationRecord = CKRecord(recordType:「location」)@matt – Steve

+0

嗯,這是你的問題。如果您想修改_existing_記錄,爲什麼要製作_new_記錄? – 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) 
       } 
      })