【问题标题】:How to connect to local Firebase-server via Swift?如何通过 Swift 连接到本地 Firebase 服务器?
【发布时间】:2017-04-22 21:16:20
【问题描述】:

我真的很想通过使用firebase-server 的本地安装来编写一些集成测试,这将模拟实时数据库服务器。

我有 installed 它并从 CLI 启动它: node_modules/.bin/firebase-server -p 5000

但我无法通过 Swift 连接到它。 按照 Firebase 的文档,需要在项目中下载使用 GoogleService-Info.plist。这显然必须改变,因为我想连接到本地服务器。

这就是我现在在AppDelegate.swift 中所拥有的:

var firebaseConfig: String!
        if isRunningTests() {
            // unit test server (idealy local)
            firebaseConfig = Bundle.main.path(forResource: "GoogleService-Info-tests", ofType: "plist")
        } else {
            #if DEBUG
                // staging server
                firebaseConfig = Bundle.main.path(forResource: "GoogleService-Info", ofType: "plist")
            #else
                // production server
                firebaseConfig = Bundle.main.path(forResource: "GoogleService-Info-prod", ofType: "plist")
            #endif
        }

        guard let options = FIROptions(contentsOfFile: firebaseConfig) else {
            fatalError("Invalid Firebase configuration file.")
        }

        FIRApp.configure(with: options)
        FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions)
        FIRDatabase.database().persistenceEnabled = true

这意味着对于单元测试,我会加载GoogleService-Info-tests plist 并连接到远程测试数据库。我怎样才能连接到 ws://test.firebase.localhost:5000 ?我不清楚options 中的哪些变量是强制性的并且需要提供。

【问题讨论】:

标签: ios swift firebase firebase-realtime-database


【解决方案1】:

你可以使用FIROptions init!(googleAppID: String!, bundleID: String!, gcmSenderID GCMSenderID: String!, apiKey APIKey: String!, clientID: String!, trackingID: String!, androidClientID: String!, databaseURL: String!, storageBucket: String!, deepLinkURLScheme: String!)方法,传入真实应用的AppId、bundleId、SenderID,然后是databaseURL,可以是本地URL。您应该能够将 nil 传递给其他人并且没问题(尽管您可能必须使编译器警告静音,因为它们在 Swift 中似乎被标记为不可为空)。

或者您可以更改GoogleService-Info-tests.plist 中的一个字段以指向新数据库。

【讨论】:

  • 谢谢迈克。它通常工作,但我经常在详细模式下看到消息firebase-server Send failed: Error: not opened +0ms。通常observeSingleEvent() 不能可靠地工作。
  • 我会在该项目 (github.com/urish/firebase-server) 的问题跟踪器中提交一个错误,因为这听起来像是一个问题,而不是 Firebase 本身。请注意,firebase-server 是一个开源项目,Firebase 不支持。
  • 当然。谢谢,会的。
猜你喜欢
  • 2019-04-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-15
  • 2011-09-25
相关资源
最近更新 更多