首页 > 解决方案 > Mapbox Snapshotter 和 Swift 并发

问题描述

我正在尝试将 Mapbox Snapshotter API 与新的 Swift 并发一起使用。这是使用的代码:

func snapshot(completion: @escaping (Result<Data, Error>) -> Void) {
    // define snapshotter options

    let snapshotter = MGLMapSnapshotter(options: options)
    snapshotter.start { (image, error) in
        if let error = error {
            completion(.failure(error))
        }

        guard let snapshot = image else { return }
        if let imageData = snapshot.image.jpegData(compressionQuality: 0.75) {
            completion(.success(imageData))
       }
   }
}

func pathSnapshot() async -> Data {
    do {
        return try await withCheckedThrowingContinuation { continuation in
            pathSnapshot() { result in
                switch result {
                case .failure(let error):
                    continuation.resume(throwing: error)
                case .success(let data):
                    continuation.resume(returning: data)
                }
            }
        }
    } catch {
        print("error")
        return Data()
    }
}

snapshot(completion: @escaping (Result<Data, Error>) -> Void) 的代码与经典的完成处理程序配合得很好。

不幸的是,使用 Swift 并发,这会导致崩溃并出现以下错误:线程 3:EXC_BAD_ACCESS (code=EXC_I386_GPFLT)。

有什么线索吗?

标签: swiftmapbox-ios

解决方案


推荐阅读