ios - swift 从 Cloudkit 中删除 CKRecord

标签 ios swift function cloudkit ckrecord

我正在尝试通过此功能更新用户的位置。最初它每次都是保存用户的位置,但我添加了评论下方的代码。我怎样才能确保被删除的记录是旧位置?

let locationRecord = CKRecord(recordType: "location")

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!
            }
        //testing code below
        let operation = CKModifyRecordsOperation(recordsToSave: nil, recordIDsToDelete: [locationRecord.recordID])
        operation.savePolicy = .AllKeys
        operation.modifyRecordsCompletionBlock = { added, deleted, error in
            if error != nil {
                print(error)
            } else {
                print("location updated")
            }
        }
        CKContainer.defaultContainer().publicCloudDatabase.addOperation(operation)
    }

最佳答案

正如@Michael 指出的那样,最好的方法是更新现有的。执行此操作的简单方法是在您第一次创建记录时,将 recordID 保存为默认值。例如,

func getRecordToUpdate(locations:[CLLocation])->CKRecord?{
  let defaults = NSUserDefaults.standardUserDefaults()
  var locationRecord:CKRecord

  if defaults.objectForKey("locationRecordID") == nil{
      //Create a new record and save its id
      locationRecord = CKRecord(recordType: "location")
      defaults.setObject(locationRecord.recordID, forKey: "locationRecordID")
      self.updateLocationRecord(locationRecord, locations)
  }else{
      //Fetch the already existing record
      let publicDB = CKContainer.defaultContainer().publicCloudDatabase
      publicDB.fetchRecordWithID((defaults.objectForKey("locationRecordID") as! CKRecordID), completionHandler{
          (record, error) in
          if error == nil{
            self.updateLocationRecord(record, locations)
          }else{
            print("Error fetching previous record")
          }
      }
  }
}

创建一个新函数,

func updateLocationRecord(locationRecord:CKRecord, locations:[CLLocation]){
   //Put your code from "let location..." to just above the testing code
   //You may want to fix the block syntax in the call to saveRecord
}

然后在原来的locationManager方法中,把所有东西都取出来

self.getRecordsToUpdate(locations)

关于ios - swift 从 Cloudkit 中删除 CKRecord,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39341212/

相关文章:

ios - 如何在所有 UIViewControllers 中将导航栏中的图像居中? swift/objective-C

python - 如何将函数应用于多个 Pandas 数据框

ios - 防止登录 Swift/Parse 中的 Segue 转换

ios - 从另一个 View Controller 检索 SQLite 数据

swift - Swift 中有 Object 类吗?

ios - 函数不允许我使用#named : string

c - 将来自多个函数的数据存储在数组中

matlab - matlab将4字节转换为 float

ios - 如何防止 SKAction 序列在解码后重新启动?

ios - 如何使用 KVO 传递值?