私はDispatchGroup.enter()とleave()を使ってヘルパークラスのreverseG async関数を処理しています。問題は明らかです、私はmainViewControllerのオブジェクトを使用して、mainViewControllersのヘルパクラスのdispatchGroup.leave()を呼び出しています!それを行う方法はありますか?swift 3 DispatchGroup leaveはヘルパークラス関数で呼び出されるとクラッシュする
同じコードは、メインビューコントローラでreverseGが宣言されているときに機能します。メインビューコントローラから
class Geo {
var obj = ViewController()
static func reverseG(_ coordinates: CLLocation, _ completion: @escaping (CLPlacemark) ->()) {
let geoCoder = CLGeocoder()
geoCoder.reverseGeocodeLocation(coordinates) { (placemarks, error) in
if let error = error {
print("error: \(error.localizedDescription)")
}
if let placemarks = placemarks, placemarks.count > 0 {
let placemark = placemarks.first!
completion(placemark) // set ViewController's properties
} else {
print("no data")
}
obj.dispatchGroup.leave() // ** ERROR **
}
}
}
関数呼び出し
dispatchGroup.enter()
Geo.reverseG(coordinates, setValues) // completionHandler: setValues
dispatchGroup.notify(queue: DispatchQueue.main) {
// call another function on completion
}
すべての 'leave'コールは、関連する' enter'コールを持っていなければなりません。あなたが最初の 'enter'と呼ばれることなく、' leave'呼び出す場合ここで問題となっているのは、あなたがいくつかのグループで 'enter'を呼び出しているということですが、' reverseG'は 'ViewController'の他のインスタンスで' leave'を呼び出しています。 'DispatchGroup'をパラメータを 'reverseG'メソッドに渡します。グループを離れるべきではなく、 'reserveG'が呼び出す完了ハンドラの中に' leave'コールを入れてください。 – Rob
まあ、すでにそれをして、それは働いた。ありがとう –