我对 SwiftUI 和 Firebase 还是很陌生。最近,作为一个爱好,我一直在为我的学校开发一个应用程序。在 Xcode 12 发布后,我决定尝试一些新功能,例如 Widgets。但是,由于我的应用程序从 Firebase 获取数据,我遇到了一些问题。我最近的问题是这个“线程 1:”无法获取 FirebaseApp 实例。请在使用 Firestore 之前调用 FirebaseApp.configure()。我不完全确定将“FirebaseApp.configure()”放在哪里,因为小部件没有 AppDelegate.swift。我的代码如下。
编辑:我重新排列了我的代码,以便我现在从原始 iOS 应用程序数据模型中获取数据。因此,我没有在小部件 Swift 文件中导入 Firebase。但是,我仍然收到相同的错误(“SendProcessControlEvent:toPid:遇到错误:Error Domain=com.apple.dt.deviceprocesscontrolservice Code=8”和“-> 0x7fff5bb6933a <+10>: jae 0x7fff5bb69344 ; <+20> -线程 1:“无法获取 FirebaseApp 实例。请在使用 Firestore 之前调用 FirebaseApp.configure()”)。我还包含了@Wendy Liga 的代码,但我仍然遇到同样的错误。我的新代码如下:
iOS 应用数据模型
import Foundation
import SwiftUI
import Firebase
import FirebaseFirestore
struct Assessment: Identifiable {
var id:String = UUID().uuidString
var Subject:String
var Class:Array<String>
var Day:Int
var Month:String
var Title:String
var Description:String
var Link:String
var Crit:Array<String>
}
class AssessmentsViewModel:ObservableObject {
@Published var books = [Assessment]()
private var db = Firestore.firestore()
// Add assessment variables
@Published var AssessmentSubject:String = ""
//@Published var AssessmentClass:Array<String> = [""]
@Published var AssessmentDay:Int = 1
@Published var AssessmentMonth:String = "Jan"
@Published var AssessmentTitle:String = ""
@Published var AssessmentDescription:String = ""
@Published var AssessmentLink:String = ""
@Published var AssessmentCrit:Array<String> = [""]
@Published var AssessmentDate:Date = Date()
func fetchData() {
db.collection("AssessmentsTest").order(by: "date").addSnapshotListener { (QuerySnapshot, error) in
guard let documents = QuerySnapshot?.documents else {
print("No documents")
return
}
self.books = documents.map { (QueryDocumentSnapshot) -> Assessment in
let data = QueryDocumentSnapshot.data()
let Subject = data["subject"] as? String ?? ""
let Class = data["class"] as? Array<String> ?? [""]
let Day = data["day"] as? Int ?? 0
let Month = data["month"] as? String ?? ""
let Title = data["title"] as? String ?? ""
let Description = data["description"] as? String ?? ""
let Link = data["link"] as? String ?? ""
let Crit = data["crit"] as? Array<String> ?? [""]
return Assessment(Subject: Subject, Class: Class, Day: Day, Month: Month, Title: Title, Description: Description, Link: Link, Crit: Crit)
}
}
}
func writeData() {
let DateConversion = DateFormatter()
DateConversion.dateFormat = "DD MMMM YYYY"
let Timestamp = DateConversion.date(from: "20 June 2020")
db.collection("AssessmentsTest").document(UUID().uuidString).setData([
"subject": AssessmentSubject,
"month": AssessmentMonth,
"day": AssessmentDay,
"title": AssessmentTitle,
"description": AssessmentDescription,
"link": AssessmentLink,
"crit": AssessmentCrit,
"date": AssessmentDate
]) { err in
if let err = err {
print("Error writing document: \(err)")
} else {
print("Document successfully written!")
}
}
}
}
小部件视图
struct WidgetsMainView: View {
@ObservedObject private var viewModel = AssessmentsViewModel()
var body: some View {
HStack {
Spacer().frame(width: 10)
VStack(alignment: .leading) {
Spacer().frame(height: 10)
ForEach(self.viewModel.books) { Data in
HStack {
VStack {
Text(String(Data.Day))
.bold()
.font(.system(size: 25))
Text(Data.Month)
}
.padding(EdgeInsets(top: 16, leading: 17, bottom: 16, trailing: 17))
.background(Color(red: 114/255, green: 112/255, blue: 110/255))
.foregroundColor(Color.white)
.cornerRadius(10)
VStack(alignment: .leading, spacing: 0) {
Text("\(Data.Subject) Crit \(Data.Crit.joined(separator: " + "))")
.bold()
if Data.Title != "" {
Text(Data.Title)
} else {
Text(Data.Class.joined(separator: ", "))
}
}
.padding(.leading, 10)
}
}
.onAppear {
viewModel.books.prefix(2)
}
Spacer()
}
Spacer()
}
}
}
小部件@main
class AppDelegate: NSObject, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
FirebaseApp.configure()
return true
}
}
@main
struct AssessmentsWidget: Widget {
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
private let kind: String = "Assessments Widget"
public var body: some WidgetConfiguration {
StaticConfiguration(kind: kind, provider: Provider(), placeholder: PlaceholderView()) { entry in
AssessmentsWidgetEntryView(entry: entry)
}
.configurationDisplayName("Assessments Widget")
.description("Keep track of your upcoming assessments.")
.supportedFamilies([.systemMedium])
}
}