【发布时间】:2021-03-09 04:19:32
【问题描述】:
我正在开发 SwiftUI 上的患者/库存管理软件,我决定放弃 CoreData 以使用 Firebase。这似乎不那么麻烦,但对于初学者来说,这仍然是一个学习曲线。开发人员最初帮助我创建并获取 API 患者文件中的患者,但现在我正在尝试更新数据,我似乎无法弄清楚要写什么......
我浏览了多个教程,它们似乎都在使用 db.collection 做事的方式。
这是我的代码
import Foundation
import FirebaseFirestore
class PatientAPI {
func createPatient(firstName: String, lastName: String, address: String, postCode: String, phoneNumber: String, onSuccess: @escaping(_ successMessage: String) -> Void) {
let testPatient = PatientModel.init(firstName: firstName, lastName: lastName, address: address, postalCode: postCode, phoneNumber: phoneNumber)
guard let dict = try? testPatient.toDictionary() else {return} /
API.FIRESTORE_COLLECTION_PATIENTS.addDocument(data: dict)
onSuccess("Done")
}
func getPatients(onSuccess: @escaping(_ patients: [PatientModel]) -> Void, onError: @escaping(_ errorMesage: String) -> Void) {
API.FIRESTORE_COLLECTION_PATIENTS.getDocuments { (snapshot, error) in
if let error = error {
onError(error.localizedDescription)
return
}
guard let snap = snapshot else {return}
var patients = [PatientModel]()
for document in snap.documents {
let dict = document.data()
guard let decodedPatient = try? PatientModel.init(fromDictionary: dict) else {return}
patients.append(decodedPatient)
}
onSuccess(patients)
}
}
}
非常感谢
【问题讨论】:
标签: swift firebase google-cloud-firestore swiftui foundation