gpt4 book ai didi

swift - 如何修复每个中的 geocodeAddressString 闭包

转载 作者:行者123 更新时间:2023-11-30 10:44:50 25 4
gpt4 key购买 nike

我在数据库中有任务模型(使用领域),其中包含 id、标题、距离、经度、纬度、customerAddress。我正在尝试更新我的任务距离。我是 swift 新手,所以我不明白应该如何修复 geoCoder.geocodeAddressString 闭包,以便所有任务都会随距离更新。 (当任务没有纬度和经度时,我使用 geocodeAddressString 检查任务是否有 customeradress

    func updateTasksDistance() {
// get tasks for db
guard let tasks = Task.getAllUserTasks() else { return }

// last tracked location
guard let lastLocation = lastLocation else { return }

let myLocation = CLLocation(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)

var distance = 0

tasks.forEach({ (task) in
// check if task has longitude and latitude
if let lat = Double(task.latitude), let long = Double(task.longitude), lat != 0 && long != 0 {
let taskLocation = CLLocation(latitude: lat, longitude: long)
distance = Int(taskLocation.distance(from: myLocation))
} else if !task.customerAddress.isEmpty { // check if task has address
geoCoder.geocodeAddressString(task.customerAddress) { placemarks, _ in
if let placemark = placemarks?.first, let location = placemark.location {
self.taskLocationCoordinate = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude )
}
}
}

// check if we have closure location??
if let taskLocation = taskLocationCoordinate {
distance = Int(CLLocation(latitude: taskLocation.coordinate.latitude, longitude: taskLocation.coordinate.longitude).distance(from: myLocation))

taskLocationCoordinate = nil
}

// update my distance to task
updateTaskDistanceDb(task: task, with: distance)

// reset distance
distance = 0
})
}

// update task distance in db
fileprivate func updateTaskDistanceDb(task: Task, with distance: Int) {
let realm = try? Realm()

if let realm = realm {
do {
try realm.write {
task.distance = distance
}
} catch {
print("error")
}
}
}

当前结果:在未调用闭包的情况下,距离会正确更新,但是当调用闭包时,我会得到乱序结果

预期结果:所有任务相对于我的距离都已正确更新

最佳答案

使用以下代码修复了此问题:

   fileprivate func geoCode(addresses: [String], results: [CLPlacemark] = [], completion: @escaping ([CLPlacemark]) -> Void ) {
guard let address = addresses.first else {
completion(results)
return
}

let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(address) { placemarks, _ in
var updatedResults = results

if let placemark = placemarks?.first {
updatedResults.append(placemark)
}

let remainingAddresses = Array(addresses[1..<addresses.count])
self.geoCode(addresses: remainingAddresses, results: updatedResults, completion: completion)
}
}

func updateTasksDistance() {
// get tasks for db
guard let tasks = Task.getAllUserTasks() else { return }

// last tracked location
guard let lastLocation = lastLocation else { return }

let myLocation = CLLocation(latitude: lastLocation.coordinate.latitude, longitude: lastLocation.coordinate.longitude)

let dispatchGroup = DispatchGroup()
for task in tasks where !task.customerAddress.isEmpty {

let addresses = [task.customerAddress]

dispatchGroup.enter()
geoCode(addresses: addresses) { results in
guard let customerAdress = results.first else { return }
guard let customerLocatin = customerAdress.location else { return }

let taskLocation = CLLocation(latitude: customerLocatin.coordinate.latitude,
longitude: customerLocatin.coordinate.longitude )
// do additional sutff
dispatchGroup.leave()
}
}

dispatchGroup.notify(queue: DispatchQueue.main, execute: {
// got all the address
}
})
}

递归地理编码函数帮助计算所有坐标,dispatchGroup.notify用于等待所有地址都被地理编码。

关于swift - 如何修复每个中的 geocodeAddressString 闭包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55958862/

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