从当前 iCloud 用户获取 CloudKit 条记录

Fetch CloudKit records from current iCloud user

我想获取当前登录用户的所有位置记录。

这会在 CloudKit 上创建位置记录:

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last!
    let center = CLLocationCoordinate2D(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
    addCrumbPoint(center)
    let locationRecord = CKRecord(recordType: "location")
    locationRecord["location"] = location
    let publicData = CKContainer.defaultContainer().publicCloudDatabase
    publicData.saveRecord(locationRecord) { record, error in
    }
}

但是我如何才能只提取当前登录用户的位置记录呢?

我想用它在地图上创建以前旅程的面包屑,但仅仅让它打印一个列表就是一个很好的开始!

到目前为止,这是我的代码:

 func getLocationAsync(complete: (instance: CKRecordID?, error: NSError?) -> ()) {
    let container = CKRecordID(recordName: "Location")
    publicDB.fetchRecordWithID(location) { fetchedLocation, error in
        if error != nil {
            print(error!.localizedDescription)
            complete(instance: nil, error: error)
        } else {
            print("fetched Location \(recordID?.recordName)")
            complete(instance: recordID, error: nil)
        }
    }
}

如果您的目标是保存用户的面包屑位置而不与其他用户共享,请使用私有数据库。 这样就可以检索到用户的所有位置记录。

如果要使用public DB,则将CKReference 类型的条目添加到指向用户记录的Location 记录。这样就可以使用基于用户 recordID

的谓词