首页 > 解决方案 > iOS - Firestore 多次检索相同的数据

问题描述

我的目标:从 Firestore 中检索数据并显示一次所述数据。

问题:多次检索相同的数据。如果有 5 行数据,我的模型数组检索 5 次。为什么会这样?

这是我的 Firestore 模型:

"Users - Investors"
   "currentUser.uid"
      "id": currentUser
      "fullName": fullName,
      "username": username,
      "userLocation": userLocation,
      "birthday": birthday

这是我的模型:

struct InvestorModel: Identifiable {
    let id: String
    let fullName: String
    let username: String
    let userLocation: String
    let birthday: Date
}

这是我的虚拟机:

class InvestorProfileViewModel: ObservableObject {
    @Published var investorProfile: [InvestorModel] = []
    
    private var auth = Auth.auth()
    private var store = Firestore.firestore()
    
    func getUserInfo() {
        let currentUser = auth.currentUser?.uid ?? ""
        store.collection("Users - Investors").document(currentUser).addSnapshotListener { snapshot, error in
            guard let data = snapshot?.data(), error == nil else {
                print("NO INVESTOR DATA, TRYING ARTIST DATA")
                return
            }

            DispatchQueue.main.async {
                self.investorProfile = data.map { (dataSnapshot) -> InvestorModel in
                    return InvestorModel(
                        id: currentUser,
                        fullName: data["fullName"] as? String ?? "",
                        username: data["username"] as? String ?? "",
                        userLocation: data["userLocation"] as? String ?? "",
                        birthday: data["birthday"] as? Date ?? Date()
                    )
                }
                print(self.investorProfile)
            }
        }
    }
}

这是我的看法:

struct HomeView: View {
    @EnvironmentObject var hvm: HomeViewModel
    @EnvironmentObject var ipvm: InvestorProfileViewModel
    @Environment(\.colorScheme) var colorScheme
    
    let devInvestor = DeveloperPreview.instance.investor
    
    var body: some View {
        ZStack {
            Color.theme.background.ignoresSafeArea(.all)
            VStack {
                ForEach(ipvm.investorProfile) { investor in
                    MenuContent(investor: investor, menuWidth: menuWidth)
                }
            }
            .padding(.vertical, 20)
            .padding(.horizontal, 30)
        }
        .onAppear {
            self.ipvm.getUserInfo()
        }
    }
}

getUserInfo() 内部的打印调用返回:

[musifi.InvestorModel(id: "F65D7CB4-25B3-47C5-9A26-78C645916AAC", fullName: "John Doe", username: "johnn", userLocation: "Lake Elsinore, CA, USA", 生日: 2021-10-15 00:59:47 +0000),

[musifi.InvestorModel(id: "F65D7CB4-25B3-47C5-9A26-78C645916AAC", fullName: "John Doe", username: "johnn", userLocation: "Lake Elsinore, CA, USA", 生日: 2021-10-15 00:59:47 +0000),

musifi.InvestorModel(id: "2B4AE47A-4C55-4229-AB9F-1F2BA9983B44", fullName: "John Doe", username: "johnn", userLocation: "Lake Elsinore, CA, USA", 生日: 2021-10-15 00 :59:47 +0000),

musifi.InvestorModel(id: "E8F87591-DC12-4068-958E-D2DA9025C316", fullName: "John Doe", username: "johnn", userLocation: "Lake Elsinore, CA, USA", 生日: 2021-10-15 00 :59:47 +0000),

musifi.InvestorModel(id: "95C8B099-C753-410E-AF7D-72EDD8FB0E7D", fullName: "John Doe", username: "johnn", userLocation: "Lake Elsinore, CA, USA", 生日: 2021-10-15 00 :59:47 +0000)]

4 种类型的数据:fullName、username、userLocation、birthday - 以及被调用的 4 次。这个数字总是相关的。我只调用该函数一次。

标签: iosswiftfirebasegoogle-cloud-firestorecombine

解决方案


我仍然不完全确定为什么会这样,但确实如此。.map 可能正在迭代每一行。- 请评论任何理论,以便我更好地理解,谢谢!

    func getInvestorProfile() {
        store.collection("Users - Investors").document(auth.currentUser?.uid ?? "").addSnapshotListener { snapshot, error in
            guard let data = snapshot?.data(), error == nil else {
                print("NO INVESTOR DATA, TRYING ARTIST DATA")
                return
            }

            DispatchQueue.main.async {
                self.investorProfile.append(
                    InvestorModel(id: data["id"] as? String ?? "",
                                  fullName: data["fullName"] as? String ?? "",
                                  username: data["username"] as? String ?? "",
                                  userLocation: data["userLocation"] as? String ?? "",
                                  birthday: data["birthday"] as? Date ?? Date()
                                 )
                )
                print(self.investorProfile)
            }
        }
    }

推荐阅读