[英]How do I pass data from an array query to a view file?
Since my last question was poorly described and not really a question, I've decided to re-brand my question.由于我的最后一个问题描述得不好而且不是一个真正的问题,所以我决定重新命名我的问题。
What is the best way to pass an array from my Firestore Database to my view file from my model file?将数组从我的 Firestore 数据库传递到我的 model 文件中的视图文件的最佳方法是什么? Is my approach even correct?我的方法是否正确?
Essentially three files are used to create the output.基本上三个文件用于创建 output。 The code I used was from another function that does work, though it is a different query.我使用的代码来自另一个确实有效的 function,尽管它是一个不同的查询。
import SwiftUI
import Firebase
import FirebaseFirestore
struct Friends_Requests: View {
@State var goBack: Bool = false
@ObservedObject var friends_model = Friends_Model()
var body: some View {
if !goBack {
VStack {
Group{
Text("Your Friend Requests")
.font(.title)
.fontWeight(.bold)
Divider()
}
Spacer()
List (friends_model.friend_requests) { item in
Text(item)
}
.refreshable{
friends_model.getFriendRequestData()
}
.listStyle(.plain)
Spacer()
Group {
HStack{
Text("Friends requests will appear here")
}
}
Group {
Divider()
HStack {
Button(action: {
withAnimation {
self.goBack.toggle()
}
}) {
Image(systemName: "arrow.left.circle.fill")
.resizable()
.frame(width: 40, height: 40)
}
.padding(.leading, 20)
Spacer()
}
}
}
} else if goBack {
FriendsPanel()
}
}
struct Friends_Requests_Previews: PreviewProvider {
static var previews: some View {
Friends_Requests()
}
}
}
import Foundation
import Firebase
import FirebaseFirestore
import SwiftUI
class Friends_Model: ObservableObject {
@Published var friend_requests = [Friends_Requests_Data]()
func getFriendRequestData() {
let db = Firestore.firestore()
let user = Auth.auth().currentUser
let email = user?.email ?? "error: email"
let queryRef = db.collection("users").document(email)
queryRef.getDocument { snapshot, error in
//check for errors
if error == nil {
print("no errors")
if let snapshot = snapshot {
self.friend_requests = snapshot.get("friendRequests") { d in
//Create friend item for each document
return Friends_Requests_Data(id: d.documentID,
email: d["friendRequests"] as? String ?? "")
}
} else {
// handle error
}
}
}
}
import Foundation
struct Friends_Requests_Data: Identifiable {
var id: String
var email: String
}
Thanks to the help of the users above I managed to get the whole thing working.感谢上面用户的帮助,我设法让整个事情顺利进行。
I changed my View model function to look like this (Friends_Model.swift) :我改变了我的视图 model function 看起来像这样(Friends_Model.swift) :
func getFriendRequestData() {
let db = Firestore.firestore()
let user = Auth.auth().currentUser
let email = user?.email ?? "error: email"
let queryRef = db.collection("users").whereField("friendRequests", arrayContains: email)
queryRef.getDocuments { snapshot, error in
//check for errors
if error == nil {
print("no errors")
if let snapshot = snapshot {
//Update the list property in main thread
//DispatchQueue.main.async {
//get all docs and create friend list
self.friend_requests = snapshot.documents.map { d in
//Create friend item for each document
return Friends_Requests_Data(id: d.documentID,
friendRequests: d["email"] as? String ?? "")
}
//}
}
} else {
// handle error
}
}
}
I also changed this in my view (Friends_Requests.swift) :在我看来,我也改变了这一点(Friends_Requests.swift) :
init() {
friends_model.getFriendRequestData()
}
List (friends_model.friend_requests) { item in
Text(item)
}
(New) (新的)
List (friends_model.friend_requests) { item in
Text(item.friendRequests)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.