如何在 iOS 16+ 中进行 CloudKit 查询操作?

问题描述 投票:0回答:1

CKQueryOperation
recordFetchBlock
queryCompletionBlock
已被弃用。我现在如何完成这个操作?

queryOperation.recordFetchedBlock = { record in
    if let location = record["location1"] as? CLLocation {
        var hideEvent: Bool = false
        if let takeaway = record["host"] as? String {
            for blockee in self.blockedFeed {
                if takeaway == blockee {
                    hideEvent = true
                }
            }
            for blocker in self.blockedFeeder {
                if takeaway == blocker {
                    hideEvent = true
                }
            }
            if hideEvent == false {
                let identified = record.recordID.recordName
                let category = record["type"] as? String ?? "defaultCategory"

                let feature = """
            {
                "type": "Feature",
                "properties": {
                    "category": "\(category)",
                    "recordID": "\(identified)"
            },
                "geometry": {
                    "type": "Point",
                    "coordinates": [\("\(location.coordinate.longitude)"), \("\(location.coordinate.latitude)")]
                }
            }
            """
                features.append(feature)
            }
        }
    }
}

// Completion block
queryOperation.queryCompletionBlock = { cursor, error in
    if let error = error {
        print("Error:", error)
        return
    }
    if let cursor = cursor {
        // Create a new operation with the cursor to get the next batch
        let newOperation = CKQueryOperation(cursor: cursor)
        newOperation.resultsLimit = CKQueryOperation.maximumResults
        newOperation.recordFetchedBlock = queryOperation.recordFetchedBlock
        newOperation.queryCompletionBlock = queryOperation.queryCompletionBlock
        publicDB.add(newOperation)
    } else {
        let geoJSONString = """
        {
            "type": "FeatureCollection",
            "features": [\(features.joined(separator: ","))]
        }
        """
        completion(geoJSONString.data(using: .utf8))
    }
}

publicDB.add(queryOperation)
swift deprecated cloudkit ckrecord ckquery
1个回答
0
投票

如果您在 Xcode 中使用代码完成功能,它会显示

recordFetchedBlock
的替换内容。替代品是
recordMatchedBlock

Xcode 代码完成还显示了

queryCompletionBlock
的替换(显然在 iOS 15 中已弃用)。替代品是
queryResultBlock


现在两个完成块的基本模式是:

let q = CKQueryOperation()
// other setup as needed

q.recordMatchedBlock = { recordID, result in
    switch result {
        case .success(let record):
            // Handle success
            print("RecordID: \(recordID), record: \(record)")
        case .failure(let error):
            // Handle error
            print("Failed for \(recordID): \(error)")
    }
}

q.queryResultBlock = { result in
    switch result {
        case .success(let cursor):
            // Handle success and use cursor as needed
            print("success")
        case .failure(let error):
            // Handle error
            print(error)
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.