0

I made the front end of an application but now I have to make the back end and therefore print a DB the problem is that I have an error "Failed to get FirebaseApp instance. Please call FirebaseApp.configure() before using Firestore" :

App delegate :

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

DataBase :

struct New: Identifiable {
    var id: String = UUID().uuidString
    var news: String
}

class NewsViewModel: ObservableObject {
    @Published var news = [New]()
    
    private var db = Firestore.firestore()
    
    func fetchDate() {
        db.collection("News").addSnapshotListener { (querySnapshot, error) in
            guard let documents = querySnapshot?.documents else {
                print("No documents")
                return
            }
            
            self.news = documents.map { (QueryDocumentSnapshot) -> New in
                let data = QueryDocumentSnapshot.data()
                
                let news = data["News"] as? String ?? "ya r"
                
                return New(news: news)
            }
        }
    }
}

Print DB :

        NavigationView {
            List(viewModel.news) { news in
                VStack(alignment: .leading) {
                    Text(news.news)
                }
                .navigationTitle("News")
            }
        }
        .onAppear() {
            self.viewModel.fetchDate()
        }

Thanks for help

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
tom debout
  • 69
  • 1
  • 6

1 Answers1

0

Can you add it inside willFinishLaunchingWithOptions

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
    FirebaseApp.configure()
    return true
}
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87