我有一个 CKRecord 的食谱,这些食谱会被提取到 iOS 应用程序中。获得该列表后,我需要优先考虑建议最高的“likedIds”食谱的结果,这是一个表示 recipe_ids 的整数列表(请参阅下面的我的数据集图像),然后在其下方显示剩余的食谱。我不能只按评分最高的食谱排序,因为我在项目的其他地方使用协同过滤来建议特定食谱而不是其他食谱,但我仍然想将它们全部列出。
在下面的这段代码中,我将结果附加到一个名为“查询”NSPredicates
的列表中。CKQuery
既然我有了它[CKquery]
,我该怎么办?如何获取我需要的结果?publicDB.perform()
如果不是正确的方法(因为我收到“预期的参数类型”错误),我应该使用哪种方法?下面,我尝试在AttachToMainThread()
我编写的函数中使用 for 循环,但我在刷新时得到 3 个不同的随机结果,“likedIds”列表中的食谱,不是“likedIds”列表中的食谱,或者根本没有,但没有 SIGABRT很好的错误。
TL;博士
我如何处理我的CKquery
列表来获取结果?
感谢您的时间!
只是专注于主要主题的代码:
import Foundation
import CloudKit
class Model {
// MARK: - iCloud Info
let container: CKContainer
let publicDB: CKDatabase
private(set) var recipes: [Recipe] = []
static var currentModel = Model()
init() {
container = CKContainer.default()
publicDB = container.publicCloudDatabase
privateDB = container.privateCloudDatabase
}
@objc func refresh(_ completion: @escaping (Error?) -> Void) {
var queries = [CKQuery] ()
// Function that returns a CKQuery
let likedQuery = GetRecipesWithLikedIds()
queries.append(likedQuery)
// Function that returns a CKQuery
let unlikedQuery = GetRecipesWithoutLikedIds()
queries.append(unlikedQuery)
AttachToMainThread(forQuery: queries, completion)
}
private func AttachToMainThread(forQuery queries: [CKQuery],
_ completion: @escaping (Error?) -> Void) {
for q in queries {
publicDB.perform(q,
inZoneWith: CKRecordZone.default().zoneID) { [weak self] results, error in
guard let self = self else { return }
if let error = error {
DispatchQueue.main.async {
completion(error)
}
return
}
guard let results = results else { return }
self.recipes = results.compactMap {
Recipe(record: $0, database: self.publicDB)
}
DispatchQueue.main.async {
completion(nil)
}
}
}
}
}
我的整个代码:
import Foundation
import CloudKit
class Model {
// MARK: - iCloud Info
let container: CKContainer
let publicDB: CKDatabase
var carbohydrate = "rice"
var vegetable = "tomatoes"
// MARK: - Properties
private(set) var recipes: [Recipe] = []
static var currentModel = Model()
init() {
container = CKContainer.default()
publicDB = container.publicCloudDatabase
privateDB = container.privateCloudDatabase
}
@objc func refresh(_ completion: @escaping (Error?) -> Void) {
var queries = [CKQuery] ()
let likedQuery = GetRecipesWithLikedIds()
queries.append(likedQuery)
let unlikedQuery = GetRecipesWithoutLikedIds()
queries.append(unlikedQuery)
AttachToMainThread(forQuery: queries, completion)
}
private func AttachToMainThread(forQuery queries: [CKQuery],
_ completion: @escaping (Error?) -> Void) {
for q in queries {
publicDB.perform(q,
inZoneWith: CKRecordZone.default().zoneID) { [weak self] results, error in
guard let self = self else { return }
if let error = error {
DispatchQueue.main.async {
completion(error)
}
return
}
guard let results = results else { return }
self.recipes = results.compactMap {
Recipe(record: $0, database: self.publicDB)
}
for r in self.recipes {
self.PrettyPrintRecipes(rName: r.name, rId: String(r.recipe_id), rIng: r.ingredients)
}
DispatchQueue.main.async {
completion(nil)
}
}
}
}
func GetRecipesWithLikedIds() -> CKQuery {
let searchTextA: [String] = [carbohydrate," "+carbohydrate,carbohydrate+"s",carbohydrate+"es"]
let subPred1 = NSPredicate (format: "ANY ingredients IN %@",argumentArray: [searchTextA])
let searchTextB: [String] = [vegetable," "+vegetable,vegetable+"s",vegetable+"es"]
let subPred2 = NSPredicate (format: "ANY ingredients IN %@",argumentArray: [searchTextB])
let likedIds: [Int] = [13733,32441]
let subPred3 = NSPredicate (format: "NOT (recipe_id IN %@)", likedIds)
let and_pred1 = NSCompoundPredicate(andPredicateWithSubpredicates: [subPred1, subPred2])
let and_pred2 = NSCompoundPredicate(andPredicateWithSubpredicates: [subPred3])
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [and_pred1, and_pred2])
return CKQuery(recordType: "Recipe", predicate: predicate)
}
func GetRecipesWithoutLikedIds() -> CKQuery {
let searchTextA: [String] = [carbohydrate," "+carbohydrate,carbohydrate+"s",carbohydrate+"es"]
let subPred1 = NSPredicate (format: "ANY ingredients IN %@",argumentArray: [searchTextA])
let searchTextB: [String] = [vegetable," "+vegetable,vegetable+"s",vegetable+"es"]
let subPred2 = NSPredicate (format: "ANY ingredients IN %@",argumentArray: [searchTextB])
let likedIds: [Int] = [13733,32441]
let subPred3 = NSPredicate (format: "recipe_id IN %@",argumentArray: [likedIds])
let and_pred1 = NSCompoundPredicate(andPredicateWithSubpredicates: [subPred1, subPred2])
let and_pred2 = NSCompoundPredicate(andPredicateWithSubpredicates: [subPred3])
let predicate = NSCompoundPredicate(andPredicateWithSubpredicates: [and_pred1, and_pred2])
return CKQuery(recordType: "Recipe", predicate: predicate)
}
func PrettyPrintRecipes(rName: String, rId: String, rIng: [String]) {
print("Name: "+rName)
print("Recipe_id: "+rId)
print("Ingredients:")
for s in 0..<rIng.count {
print("\t"+String(s)+": "+rIng[s])
}
}
public func printVegetable(){
print(vegetable)
}
}