简体   繁体   English

快速解码 httpsCallable 云函数响应

[英]Swift decode httpsCallable cloud functions response

I have a httpsCallable cloud function which is being called from the app.我有一个从应用程序调用的 httpsCallable 云函数。 Technically speaking it is working fine but as you can see, the code is very nested and I have the feeling that this can be handled somehow better.从技术上讲,它工作正常,但如您所见,代码嵌套非常多,我觉得可以以某种方式更好地处理它。 I also get a warning.我也收到警告。

The response result?.data is of type Any so I can not use it directly in try decoder.decode(PaymentIntent.self, from: data) since this requires Data type.响应result?.dataAny类型,所以我不能直接在try decoder.decode(PaymentIntent.self, from: data)中使用它,因为这需要Data类型。 And for this I'm using try? JSONSerialization.data(withJSONObject: result?.data)为此,我正在使用try? JSONSerialization.data(withJSONObject: result?.data) try? JSONSerialization.data(withJSONObject: result?.data)

Any idea how to decode everything to PaymentIntent model?知道如何将所有内容解码为PaymentIntent模型吗?

let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase

functions.httpsCallable("createPaymentIntent").call(data) { (result, error) in
    if let error = error as NSError? {
        completion(nil, error)
    } else {
        let data = try? JSONSerialization.data(withJSONObject: result?.data)
        
        if let data = data {
            do {
                let paymentIntent = try decoder.decode(PaymentIntent.self, from: data) // Expression implicitly coerced from 'Any?' to 'Any'
                completion(paymentIntent.clientSecret, nil)
            } catch {
                completion(nil, error);
            }
        }

        // Update

        if let data = result?.data as? NSDictionary {
            print(data)
            
            do {
                // Cannot convert value of type 'NSDictionary' to expected argument type 'Data'
                let paymentIntent = try decoder.decode(PaymentIntent.self, from: data)
                completion(paymentIntent.clientSecret, nil)
            } catch {
                completion(nil, error);
            }
        }
    
    }
}

The swift SDK for Firebase's callable functions does not return a JSON string. Firebase 的可调用函数的 swift SDK 不返回 JSON 字符串。 It returns a data type that's already been deserialized from JSON.它返回一个已经从 JSON 反序列化的数据类型。 It's going to be either a dictionary, array, or something that directly corresponds to whatever the cloud function generates.它将是字典、数组或直接对应于云 function 生成的任何内容。 You should do some step-through debugging to examine what result.data actually is, then write some code to verify that it is what you expect , and cast it in order to use it safely.您应该进行一些逐步调试以检查 result.data 实际是什么,然后编写一些代码来验证它是否是您所期望的,并对其进行转换以便安全地使用它。

You can't do this right now.你现在不能这样做。 You'll always get a Dictionary back and you'll need to instantiate the objects yourself using the contents of that Dictionary.你总是会得到一个字典,你需要使用字典的内容自己实例化对象。

However they are working on it:然而,他们正在努力:

https://github.com/firebase/firebase-ios-sdk/pull/8854 https://github.com/firebase/firebase-ios-sdk/pull/8854

Firebase added decoders meanwhile: Firebase 同时添加了解码器:

import FirebaseSharedSwift
import FirebaseFunctions

    ...

    let decoder = FirebaseDataDecoder()
    decoder.keyDecodingStrategy = .convertFromSnakeCase
    
    let result = try await functions
        .httpsCallable(endpoint, responseAs: T.self, decoder: decoder)
        .call(params)

Just replace 'T' with your class if your method is not generic.如果您的方法不是通用的,只需将“T”替换为您的类即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM