如何通过Swift连接到本地Firebase服务器?

问题描述:

我真的想通过利用本地安装firebase-server来编写一些集成测试,它将模仿实时数据库服务器。如何通过Swift连接到本地Firebase服务器?

我有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中的哪些变量是强制性的,需要提供。

+0

github上的相关问题:https://github.com/urish/firebase-server/issues/85 – urish

您可以使用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中的一个字段以指向新的数据库。

+0

感谢Mike。它通常工作,但通常我会在详细模式下看到消息“firebase-server发送失败:错误:未打开+ 0ms”。通常'observeSingleEvent()'不能可靠地工作。 – Houman

+0

我会在该项目的问题跟踪器(https://github.com/urish/firebase-server)中提出一个错误,因为它听起来像是一个问题,而不是Firebase本身。请注意,'firebase-server'是一个开源项目,不受Firebase支持。 –

+0

当然。谢谢,会做的。 – Houman