'get the Field datas according to the Ids from the Collection in the Firestore/SwiftUI

so, the problem that need to be solved. we need to be able to get the data of any user(Field) in the "users"-(Collection) according to the its ID

enter image description here

these are my attempts:

FriendProViewModel() :

@Published var uid: String
@Published var displayName: String
@Published var email: String

init(uid: String, displayName: String, email: String) {
    self.uid = uid
    self.displayName = displayName
    self.email = email
}

And to check the result:

@State var data = [FriendProViewModel]()


let db = Firestore.firestore()

var body: some View {
    VStack {
        ForEach((self.data), id: \.self.uid) { item in
            Text("\(item.displayName)")
            Text("\(item.email)")
        }
    }.onAppear {
//            self.fetchData()
        self.fetchData2()
    }
}

func fetchData() {
        // Remove previously data to prevent duplicate data
        self.data.removeAll()
        self.db.collectionGroup("users").getDocuments() {(querySnapshot, err) in
            if let err = err {
                print("Error getting documents \(err)")
            } else {
                for document in querySnapshot!.documents {
                    let uid = document.documentID
                    let displayName = document.get("displayName") as! String
                    let email = document.get("email") as! String

                    self.data.append(FriendProViewModel(uid: uid, displayName: displayName, email: email))
                }
            }
        }
    }

func fetchData2() {
    
    db.collection("users").document("pJSsQQ2qt6Xx9qqRGpVzRfWLgC33").getDocument { (document, error) in
        if let document = document, document.exists {
            let dataDescription = document.data().map(String.init(describing:)) ?? "nil"
            print("Document data: \(dataDescription)")
        } else {
            print("Document does not exist")
        }
    }
}

Here is the result I am getting for fetchData():

enter image description here

Here is the result I am getting for fetchData2():

enter image description here

fetchData() retrieves all data from all "users". fetchData2() is only getting data according to the id I gave. separately, I was unable to get the data of each according to the id-s...

I hope everyone understood the question. thank you for your attention ...



Solution 1:[1]

You are not setting up and using FriendProViewModel correctly. Read-up on how to use ObservableObject.

Re-structure your code to use a ObservableObject and its constituent structs, such as:

// a struct to hold each individual info
struct FriendModel {
    var uid: String
    var displayName: String
    var email: String
}

// an observable, to let the view know when it changes
class FriendProViewModel: ObservableObject {
    @Published var friends: [FriendModel] = []
}

Declare it like this in your View:

 @StateObject var viewModel = FriendProViewModel() // instead of `data`

and use it like this:

 viewModel.friends.append(FriendModel(uid: uid, displayName: displayName, email: email))

and

 ForEach(viewModel.friends, id: \.uid) { item in ...}

I also suggest you move the fetchData() etc... inside the FriendProViewModel

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 workingdog support Ukraine