用swift打印数据库



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

应用程序代表:

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

打印DB:

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
}

最新更新