SwiftUI - 在视图中显示从 Firebase 获取的数据? [英] SwiftUI - Show the data fetched from Firebase in view?
问题描述
Firebase 我试图在我的 SwiftUI 视图中显示从 Firestore 获取的数据,但我遇到了问题.我从 Firebase 中提取数据没有问题.但我无法按我的意愿显示数据.我使用 MVVM 架构.
Firebase I am trying to show data taken from Firestore in my SwiftUI view but I have a problem. I have no problem with pulling data from Firebase. But I cannot show the data as I want. I work with MVVM architecture.
我的模型是这样的:
struct UserProfileModel: Identifiable {
@DocumentID var id : String?
var username : String
var uidFromFirebase : String
var firstName : String
var lastName : String
var email : String
}
视图模型:
class UserProfileViewModel: ObservableObject {
@Published var user : [UserProfileModel] = []
private var db = Firestore.firestore()
func data(){
db.collection("Users").whereField("uuidFromFirebase", isEqualTo: Auth.auth().currentUser!.uid).addSnapshotListener { (snapshot, error) in
guard let documents = snapshot?.documents else {
print("No Documents")
return
}
self.user = documents.compactMap { queryDocumentSnapshot -> UserProfileModel? in
return try? queryDocumentSnapshot.data(as: UserProfileModel.self)
}
}
}
}
查看:
struct MainView: View {
@ObservedObject private var viewModel = UserProfileViewModel()
var body: some View { // -> Error: The compiler is unable to type-check this expression in reasonable time; try breaking up the expression into distinct sub-expressions
VStack{
Text(viewModel.user.username) // -> I want to do this but XCode is giving an error.
// This works but I don't want to do it this way.
List(viewModel.user) { user in
VStack{
Text(user.username)
Text(user.firstName)
Text(user.lastName)
Text(user.email)
Text(user.uidFromFirebase)
}
}
}
}
}
在标题为SwiftUI fetch data from Firebase"的视频和文章中在我观看和阅读的过程中,我一直在 List 和 ForEach 上进行叙述.但我想在任何地方使用这些数据.我和你分享了我所有的代码.我想了解如何做到这一点.
In the videos and articles titled "SwiftUI fetch data from Firebase" that I watched and read, I have always narrated on List and ForEach. But I want to use the data wherever. I shared all my code with you. I want to learn how I can do this.
推荐答案
在我看来,您真的只想让 一个 用户为其提取数据,但您已经设置使用一组用户 ([UserProfileModel]
) 设置您的 UserProfileViewModel
.有多种方法可以解决这个问题.这是一个(检查代码以获取有关正在发生的事情的内联注释):
Looks to me like you really just want to have one user that you're pulling the data for, but you've set up your UserProfileViewModel
with an array of users ([UserProfileModel]
). There are a number of ways that you could take care of this. Here's one (check the code for inline comments about what is going on):
class UserProfileViewModel: ObservableObject {
@Published var user : UserProfileModel? = nil // now an optional
private var db = Firestore.firestore()
func data(){
db.collection("Users").whereField("uuidFromFirebase", isEqualTo: Auth.auth().currentUser!.uid).addSnapshotListener { (snapshot, error) in
guard let documents = snapshot?.documents else {
print("No Documents")
return
}
self.user = documents.compactMap { queryDocumentSnapshot -> UserProfileModel? in
return try? queryDocumentSnapshot.data(as: UserProfileModel.self)
}.first // Take the first document (since there probably should be only one anyway)
}
}
}
struct MainView: View {
@ObservedObject private var viewModel = UserProfileViewModel()
var body: some View {
VStack {
if let user = viewModel.user { //only display data if the user isn't nil
Text(user.username)
Text(user.firstName)
Text(user.lastName)
Text(user.email)
Text(user.uidFromFirebase)
}
}
}
}
我想说一种更传统的处理方式可能是存储您的用户配置文件Users/uid/
——这样您就可以只使用用户document(uid)
来查找它而不是 whereField
查询.
I'd say a more traditional way of handling this might be to store your user profile document Users/uid/
-- that way you can just user document(uid)
to find it rather than the whereField
query.
这篇关于SwiftUI - 在视图中显示从 Firebase 获取的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!