【问题标题】:Set useFunctionsEmulator(...) globally in iOS Firebase SDK in Swift?在 Swift 的 iOS Firebase SDK 中全局设置 useFunctionsEmulator(...)?
【发布时间】:2021-03-14 23:19:39
【问题描述】:

目前我使用functions.useFunctionsEmulator(origin: "http://localhost:5001")Functions.functions() 的每次调用更新模拟器链接,这意味着我可能会引入错误。有没有办法将此设置全局应用于所有 firebase Functions.functions() 调用?

let functions = Functions.functions()
// Uncomment to hit local emulator.
// functions.useFunctionsEmulator(origin: "http://localhost:5001")
functions.httpsCallable("acceptParticipantRequest")
    .call([
        // params
    ]) { (result, error) in
        // handle results
    }

【问题讨论】:

    标签: ios swift iphone firebase google-cloud-functions


    【解决方案1】:

    根据现有的文档设置useEmulator (or useFunctionsEmulator) global 似乎不是一个选项。

    但是,通过调用它会将设置保留在 FirebaseFunctions 对象中。因此,如果您重用该对象,它将被持久化。

    【讨论】:

      【解决方案2】:

      最后在Functions 上写了一个扩展,见下面的代码。现在,如果有人在我的代码库中直接使用Functions.functions(),如果有人有办法让编译器发出警告,请在下面评论。

      extension Functions {
          /**
           * Use this to access Functions instance to avoid accidentally
           * leaving emulator wiring in prod.
           */
          static func sharedFunctions() -> Functions {
              let functions = Functions.functions()
              // Uncomment to use local emulator.
              //functions.useFunctionsEmulator(origin: "http://localhost:5001")
              return functions;
          }
      }
      

      这是一个示例调用:

      Functions.sharedFunctions().httpsCallable("invite").call(...)
      

      【讨论】:

        猜你喜欢
        • 2023-04-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-04-03
        相关资源
        最近更新 更多