快速打印数据库

问题描述

我制作了一个应用程序的前端,但现在我必须制作后端,因此打印一个数据库问题是我有一个错误“无法获取 FirebaseApp 实例。请在使用 Firestore 之前调用 FirebaseApp.configure() ":

应用委托:

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

数据库

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)
            }
        }
    }
}

打印数据库

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

感谢帮助

解决方法

你能把它加到willFinishLaunchingWithOptions内吗

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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...