从iOS应用程序调用云函数如下所示:
var functions = Functions.functions()
functions.httpsCallable(name).call(data){ (result, error) in
// Code here
}
我如何从我的云函数发送错误,这样它就会在上面的代码中被发现?例如:
云功能:
exports.joinGame = functions.https.onCall((data, context) => {
return Error("The game is full")
})
SwiftUI:
var functions = Functions.functions()
functions.httpsCallable("joinGame").call(data){ (result, error) in
print(error) // "The game is full"
}
我仍然不确定您想在哪里返回错误。。。但这里有一些可能处理的变体:
@State private var error: Error?
var body: some View {
VStack {
Text("Wait for Error")
.onAppear {
functions.httpsCallable("joinGame").call(data){ (result, error) in
print(error) // "The game is full"
DispatchQueue.main.async {
self.error = error
}
}
}
if nil != error {
Text("Got error!") // << do anything with error here
}
}
.onChange(of: error) { newError in
// << or here
}
}