【问题标题】:iOS - Firestore same data retrieving multiple timesiOS - Firestore 多次检索相同的数据
【发布时间】:2021-12-03 07:39:15
【问题描述】:

我的目标:从 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", 用户名: "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 种类型的数据:全名、用户名、用户位置、生日 - 以及被调用的 4 次。这个数字总是相关的。我只调用该函数一次。

【问题讨论】:

  • 试试@ObservedtObject var ipvm: InvestorProfileViewModel
  • @Rob 对我没用,它必须与 .map 有关 - 不引用 vm
  • 您可能想尝试的另一件事是investorProfile.append(contentsOf: YOUR MAP RESULTS)
  • @Rob 仍然没有运气,我现在非常困惑。
  • @Rob 您使用 .append() 解决了一些问题,但似乎 .map 是问题所在,这就是为什么它无法执行您的建议,感谢您为我指明正确的方向!跨度>

标签: ios swift firebase google-cloud-firestore combine


【解决方案1】:

我仍然不完全确定为什么会这样,但确实如此。 .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)
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-05
    • 2018-11-06
    • 2018-04-04
    • 2021-07-15
    • 2020-10-08
    • 1970-01-01
    相关资源
    最近更新 更多