Houman
Houman

Reputation: 66380

How to connect to local Firebase-server via Swift?

I really would like to write some integration tests by utilising a local installation of firebase-server, that would mimic the realtime database server.

I have installed it and started it from CLI: node_modules/.bin/firebase-server -p 5000

But I have trouble connecting to it via Swift. Following the docs for Firebase, it is required to download the utilise GoogleService-Info.plist in the project. This obviously has to change, as I want to connect to the local server instead.

This is what I have right now in 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

That means for unit tests I load the GoogleService-Info-tests plist and connect to a remote test database. How could I instead connect to ws://test.firebase.localhost:5000 ? It is not clear to me which variables inside options are mandatory and need to be provided.

Upvotes: 4

Views: 527

Answers (1)

Mike McDonald
Mike McDonald

Reputation: 15963

You can use the FIROptions init!(googleAppID: String!, bundleID: String!, gcmSenderID GCMSenderID: String!, apiKey APIKey: String!, clientID: String!, trackingID: String!, androidClientID: String!, databaseURL: String!, storageBucket: String!, deepLinkURLScheme: String!) method and pass in AppId, bundleId, SenderID from a real app, and then databaseURL, which can be a local URL. You should be able to pass nil into the others and be fine (though you may have to silence compiler warnings since they appear to be marked as non-nullable in Swift).

Or you can change the one field in your GoogleService-Info-tests.plist to point to the new Database.

Upvotes: 1

Related Questions