0

Upon moving a view and it's ViewModel(where i am using Firestore) from the main app module to it's own module, my Canvas Previews crash with the error:

XCPreviewAgent crashed due to an uncaught exception FIRIllegalStateException. Reason: Failed to get FirebaseApp instance. Please call FirebaseApp.configure() before using Firestore

The FirebaseApp Instance is configured in the AppDelegate:

import Firebase
import SwiftUI

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        FirebaseApp.configure()
        return true
    }
}

I have tried using Firestore in a function in the ViewModel but still crashing.

public struct AuthContentView: View {
    
    @StateObject private var vm = ViewModel()
    
    public init() {}
    
    public var body: some View {
        VStack(spacing: 24) {
            Text("AuthContentView")
            
            Button("TapMe") {
                Task {
                    await fetchData()
                }
            }
            .buttonStyle(.bordered)
        }
    }
    
    @Sendable
    func fetchData() async {
        await vm.fetchData()
    }
}

extension AuthContentView {
    @MainActor
    final class ViewModel: ObservableObject {
        
        func getFirestoreInstance() -> Firestore {
            Firestore.firestore()
        }
        
        func fetchData() async {
            let docSnap = try? await getFirestoreInstance().document("users").getDocument()
        }
    }
}
  

I have also tried configuring the FirebaseApp Instance in a dedicated Firebase module where I have conformance to protocols in Core/Domain module that allows other modules interact with the dedicated Firebase module, but the FirebaseApp.configure() cannot see the GoogleService-info.plist file which sits in the core module.

Using enter image description here

Any help is highly appreciated

1
  • 1
    the way you have your ObservableObject structured, you call Firestore.firestore() every time you call fetchData(), and at the same time you already have FirebaseApp.configure() in your class AppDelegate. It would be easier just to have your App init(){FirebaseApp.configure()}. If Xcode does not have the libraries in your project, add them manually to the target. It is also not clear why you are using final class ViewModel: ObservableObject since you don't have any @Published var .... Note, you could simply use Button("TapMe") { await vm.fetchData() }. Commented Oct 23 at 23:07

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.