gpt4 book ai didi

ios - 为什么 "self"在我的 QueryOperation 中阻止来自 CloudKit 的数据

转载 作者:行者123 更新时间:2023-11-28 15:09:38 26 4
gpt4 key购买 nike

我在 Cloudkit 的数据库中有多个 CKRecords。我将这些 CKRecords 转换为我的 map 的注释。自从我不得不在我的查询操作中的变量 var annotation = MKPointAnnotation() 之前添加 self 之后,它只向我的 map 加载了一个注释。为什么会这样,我该如何解决???任何帮助都会很棒!

我如何获取记录 -

 var points: [MKPointAnnotation] = []
var annotation = MKPointAnnotation()
let database = CKContainer.default().publicCloudDatabase

func fetchTruck() {

let truePredicate = NSPredicate(value: true)
let eventQuery = CKQuery(recordType: "User", predicate: truePredicate)
let queryOperation = CKQueryOperation(query: eventQuery)


queryOperation.recordFetchedBlock = { (record) in



self.points.append(self.annotation)

self.annotation.title = record["username"] as? String
self.annotation.subtitle = record["hours"] as? String
if let location = record["location"] as? CLLocation {
self.annotation.coordinate = location.coordinate
}

print("recordFetchedBlock: \(record)")

self.mapView.addAnnotation(self.annotation)

}




self.database.add(queryOperation)

}

最佳答案

在更详细地查看您的代码后,我认为问题在于您始终使用相同的注释。 MKPointAnnotation 是一个类,一个引用值,这意味着每次您为 self.annotation 赋值时,您都在更改引用,而不是创建一个新引用。

您正在 CKQueryOperation 闭包内修改您的应用程序 UI (mapView)。尝试在主线程中运行修改代码

尝试类似...

var points: [MKPointAnnotation] = []
let database = CKContainer.default().publicCloudDatabase

func fetchTruck()
{
let truePredicate = NSPredicate(value: true)

let eventQuery = CKQuery(recordType: "User", predicate: truePredicate)
let queryOperation = CKQueryOperation(query: eventQuery)

queryOperation.recordFetchedBlock = { (record) in
var annotation = MKPointAnnotation()

annotation.title = record["username"] as? String
annotation.subtitle = record["hours"] as? String
if let location = record["location"] as? CLLocation
{
annotation.coordinate = location.coordinate
}

self.points.append(annotation)

DispatchQueue.main.async
{
self.mapView.addAnnotation(annotation)
}

print("recordFetchedBlock: \(record)")
}

self.database.add(queryOperation)
}

关于ios - 为什么 "self"在我的 QueryOperation 中阻止来自 CloudKit 的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47882474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com