简体   繁体   English

如何使用查询快速获取Cloudkit数据

[英]How to use query to fetch cloudkit data in swift

I am trying to have all the users' locations stored in cloudkit , then downloaded by each device. 我正在尝试将所有用户的位置存储在cloudkit ,然后由每个设备下载。 I marked on the code in storeLocation where I get the error: 我在storeLocation的代码上标记了错误:

"Cannot convert value of type '(NSError?, [CKRecord]?)' (aka '(Optional, Optional>)') to expected argument type '(error: NSError?, records: [CKRecord]?) -> Void'" “无法将类型'((NSError ?, [CKRecord]?)'(aka'(Optional,Optional>)')的值转换为预期的参数类型'(error:NSError ?,记录:[CKRecord]?)->无效' “

//saves location in cloud kit //currently works well:

var locArray = [CKRecord]()
func storeLocation(location:CLLocation) {
        let locationRecord = CKRecord(recordType: "location")
        locationRecord.setObject(location, forKey: "location")
        let publicData = CKContainer.defaultContainer().publicCloudDatabase
        publicData.saveRecord(locationRecord) { (records, error) in
            if error != nil {
                print("error saving locations: \(error)")
            } else {
                print("Locations saved: \(records)")
                loadLocation((error, self.locArray)) //where I get error******
            }
        }
    }

//fetches location from cloud kit:

func loadLocation(completion: (error:NSError?, records:[CKRecord]?) -> Void)
{
    let query = CKQuery(recordType: "Location", predicate: NSPredicate(value: true))
    CKContainer.defaultContainer().publicCloudDatabase.performQuery(query, inZoneWithID: nil){
        (records, error) in
        if error != nil {
            print("error fetching locations")
            completion(error: error, records: nil)
        } else {
            print("found locations: \(records)")
            completion(error: nil, records: records)
        }
    }
}

I believe instead of: 我相信而不是:

loadLocation((error, self.locArray))

You need to call this: 您需要调用此:

loadLocation() { (error, records) in
    // do something here with the returned data.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM