You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Swift 5.5下Firebase Storage、Google Places如何适配async/await语法?

适配方案说明

你遇到的参数缺失报错,核心是当前使用的FirebaseStorage、GooglePlaces SDK版本未内置原生async/await接口,仅FirebaseFirestore较早完成了异步接口适配,因此可以直接识别try await调用。

方案1:升级SDK使用原生async接口(推荐)

直接升级对应组件到支持async/await的最低版本即可获得和Firestore一致的语法提示与调用支持:

  • FirebaseStorage 需升级到 Firebase 8.10.0 及以上版本
  • GooglePlaces 需升级到 Google Places SDK for iOS 7.0 及以上版本

升级后清理Xcode缓存(Command+Shift+K)重新编译,即可直接使用async接口,示例代码如下:

// FirebaseStorage 原生调用示例
let storageRef = Storage.storage().reference().child("文件存储路径")
do {
    // 最大下载大小设为10MB,可按需调整
    let fileData = try await storageRef.getData(maxSize: 10 * 1024 * 1024)
    // 处理返回的文件数据
} catch {
    // 捕获并处理下载错误
}

// GooglePlaces 原生调用示例
let placesClient = GMSPlacesClient.shared()
do {
    let predictions = try await placesClient.findAutocompletePredictions(
        fromQuery: "搜索关键词",
        filter: nil,
        sessionToken: nil
    )
    // 处理返回的联想预测结果
} catch {
    // 捕获并处理请求错误
}

方案2:手动封装async接口(适配低版本SDK)

如果因项目兼容要求无法升级SDK,可以通过Swift内置的CheckedThrowingContinuation工具,将带回调的旧方法手动封装为async接口:

FirebaseStorage getData 封装示例

extension StorageReference {
    func getData(maxSize: Int64) async throws -> Data {
        try await withCheckedThrowingContinuation { continuation in
            // 调用原有带回调的旧接口
            getData(maxSize: maxSize) { data, error in
                if let error = error {
                    continuation.resume(throwing: error)
                } else if let data = data {
                    continuation.resume(returning: data)
                } else {
                    continuation.resume(throwing: NSError(
                        domain: "FirebaseStorage",
                        code: -1,
                        userInfo: [NSLocalizedDescriptionKey: "未获取到有效文件数据"]
                    ))
                }
            }
        }
    }
}

GooglePlaces findAutocompletePredictions 封装示例

extension GMSPlacesClient {
    func findAutocompletePredictions(
        fromQuery query: String,
        filter: GMSAutocompleteFilter?,
        sessionToken: GMSAutocompleteSessionToken?
    ) async throws -> [GMSAutocompletePrediction] {
        try await withCheckedThrowingContinuation { continuation in
            // 调用原有带回调的旧接口
            findAutocompletePredictions(
                fromQuery: query,
                filter: filter,
                sessionToken: sessionToken
            ) { predictions, error in
                if let error = error {
                    continuation.resume(throwing: error)
                } else if let predictions = predictions {
                    continuation.resume(returning: predictions)
                } else {
                    continuation.resume(throwing: NSError(
                        domain: "GooglePlaces",
                        code: -1,
                        userInfo: [NSLocalizedDescriptionKey: "未获取到联想预测结果"]
                    ))
                }
            }
        }
    }
}

封装完成后,即可直接用try await调用封装后的方法,用法和原生async接口完全一致。

内容的提问来源于stack exchange,提问作者christianzags

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 22:24:04