Reputation: 41
I can't seem to connect to my Firestore database. The specific error I am getting is "Failed to get FirebaseApp instance. Please call FirebaseApp.configure() before using Firestore". However, I've already called the configure function. Here is my code for the section I am writing:
let db = Firestore.firestore()
I have also properly configured Firebase in AppDelegate like so
FirebaseApp.configure()
Any help on this at all would be really appreciated, I cant seem to figure out what's going on.
This is the section in reference for context:
import SwiftUI
import Firebase
import FirebaseDatabase
import Foundation
struct Practioner: Identifiable {
var id: ObjectIdentifier
var name: String
var email: String
var gender: String
var provider: String
var education: String
var subspeciality: String
var languages: String //fix this to array
var indigenous: Bool
var lgbtq: Bool
var rural: Bool
}
public struct DemographicsView: View {
@State var genderSel = ""
@State var providerSel = ""
@State var educationSel = ""
@State var subspecialtySel = ""
@State var languageSel = ""
@State var indigenousSel = false
@State var lgbtqSel = false
@State var ruralSel = false
@EnvironmentObject var settings: UserSettings
@EnvironmentObject var session: sessionStore
var genders = ["Male", "Female", "Other"]
init(){
UITableView.appearance().backgroundColor = .clear
}
public var body: some View {
VStack {
NavigationView {
Form {
Text("Demographics")
.font(.largeTitle)
.fontWeight(.semibold)
.padding(.bottom, 20)
Text("Please specify the following details about
yourself")
.font(.title)
.fontWeight(.semibold)
.padding(.bottom, 20)
Section(){
Picker(selection: $genderSel, label:
Text("Gender")) {
ForEach(0 ..< genders.count){
Text(self.genders[$0]).tag($0)
}
}
Picker(selection: $providerSel, label:
Text("Provider Type")) {
Text("Doctor").tag(1)
Text("Nurse").tag(2)
Text("Other").tag(3)
}
Picker(selection: $educationSel, label:
Text("Country of education")) {
Text("Canada").tag(1)
Text("USA").tag(2)
Text("Other").tag(3)
}
Picker(selection: $subspecialtySel, label:
Text("Subspecialty")) {
Text("OGBYN").tag(1)
Text("Family doctor").tag(2)
Text("Other").tag(3)
}
Picker(selection: $languageSel, label:
Text("Language(s)")) {
Text("English").tag(1)
Text("French").tag(2)
Text("Malayalam").tag(3)
}
}
Toggle(isOn: $indigenousSel) {
Text("Indigenous")
}
Toggle(isOn: $lgbtqSel) {
Text("LGBTQ+ (optional)")
}
Toggle(isOn: $ruralSel) {
Text("Rural")
}
Section(){
Button(action: {
let db = Firestore.firestore()
}) {
LoginButtonView(textField: "Sign up")
}.frame(minWidth: 0, maxWidth: .infinity,
minHeight: 0,maxHeight: .infinity, alignment: .center)
}
}
}.navigationBarHidden(true)
.navigationBarTitle(Text("Home"))
.edgesIgnoringSafeArea([.top, .bottom])
}
}
}
Upvotes: 4
Views: 1146
Reputation: 1401
Add
import FirebaseFirestore
at the top and try building the app again. Worked for me!
Example :
import FirebaseFirestore
import SwiftUI
import Firebase
import FirebaseDatabase
import Foundation
Upvotes: 3
Reputation: 71
In case someone comes across this while trying to run SwiftUI like previews for UIKit code. Or more specifically doing a SwiftUI UIKit integration with UIViewControllerRepresentable...
Call FirebaseApp.configure() again when overriding viewDidLoad in the view controller worked for me (even if its already in your AppDelegate). For example...
class MyController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
FirebaseApp.configure()
self.db = Firestore.firestore()
}
}
Upvotes: 0
Reputation: 35667
There are three steps to initializing and working with Firestore. It's possible one is missing (this shows the steps for Swift, SwiftUI is similar)
1) .configure in AppDelegate
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions... {
FirebaseApp.configure()
return true
}
2) Set up a class var within a ViewController (for example)
class ViewController: UIViewController {
var db: Firestore!
3) Initialize the class var in the ViewController (for example)
override func viewDidLoad() {
super.viewDidLoad()
self.db = Firestore.firestore()
Upvotes: 0
Reputation: 3426
Your declaration let db = Firestore.firestore() should be declared at the top of your Class (ViewModel) and not in a Button. There might be other mistakes, which can't be elaborated with just such little code provided.
Upvotes: 0