简体   繁体   English

Firebase 数据结构 SwiftUI

[英]Firebase Data structure SwiftUI

I am trying to figure out the best way to structure the data for my app.我正在尝试找出为我的应用程序构建数据的最佳方式。 I want it so users have a list of trips and then can go to a detail view for expenses (fuel, hotel, food, misc).我想要它让用户有一个旅行列表,然后可以通过 go 详细查看费用(燃料、酒店、食品、杂项)。

The number of trips for a user will be a lot and this is just an example there will be about 20 items on the details page.一个用户的出行次数会很多,这里只是举个例子,详情页大概有20条。 I feel like I am overthinking it but not sure if having it read all 20 items of every trip in the list just to display 2 items for each trip is a good idea.我觉得我想太多了,但不确定让它读取列表中每次旅行的所有 20 项是否只是为了每次旅行显示 2 项是个好主意。

Not sure if it will make a difference but I am using Xcode 12.4不确定它是否会有所作为,但我使用的是 Xcode 12.4

Would it be best to have the trip summary and then a subcollection of details?最好有旅行摘要,然后是详细信息的子集吗?

users:
    - UID: "randomly_generated"
    - firstName: "James"
    - email: "a@email.com"

trips:
    - startDate: "2/5/2022"
    - destination: "Orlando, FL"
    - userID: "Users UID"
    - details: []
        - food: "150"
        - hotel: "400"
        - fuel: "250"
        - misc: "75"

or should I just have it all under trips?或者我应该把它全部放在旅行中吗?

users:
    - UID: "randomly_generated"
    - firstName: "James"
    - email: "a@email.com"

trips:
    - startDate: "2/5/2022"
    - destination: "Orlando, FL"
    - userID: "Users UID"
    - food: "150"
    - hotel: "400"
    - fuel: "250"
    - misc: "75"

Try this:尝试这个:

  • users:用户:

    • FirstUID第一个UID
      • firstName: "James"名字:“詹姆斯”
      • email: "a@email.com" email: "a@email.com"
    • SecondUID第二个UID
      • firstName: "John"名字:“约翰”
      • email: "b@email.com" email:“b@email.com”
  • trips:行程:

    • FirstTripId FirstTripId
      • startDate: "2/5/2022"开始日期:“2/5/2022”
      • destination: "Orlando, FL"目的地:“佛罗里达州奥兰多”
      • uid: FirstUID uid:第一个UID
      • food: "150"食物:“150”
      • hotel: "400"酒店:“400”
      • fuel: "250"燃料:“250”
      • misc: "75"杂项:“75”
    • SecondTripId SecondTripId
      • startDate: "3/5/2022"开始日期:“2022 年 3 月 5 日”
      • destination: "Harare, Zimbabwe"目的地:“哈拉雷,津巴布韦”
      • uid: SecondUID uid:SecondUID
      • food: "21"食物:“21”
      • hotel: "69"酒店:“69”
      • fuel: "420"燃料:“420”
      • misc: "7"杂项:“7”

/// add this next part if you wish to do less queries or if you ever use rtdb /// 如果您希望减少查询次数或曾经使用过 rtdb,请添加下一部分

  • userTrips用户行程
    • FirstUID第一个UID
      • FirstTripId FirstTripId
        • startDate: "2/5/2022"开始日期:“2/5/2022”
      • NextTripIds NextTripIds
    • SecondUID第二个UID
      • SecondTripId SecondTripId
        • startDate: "3/5/2022"开始日期:“2022 年 3 月 5 日”

/// ///

When querying with Firestore you can make direct composite queries in the trips child and specify the UID you want and it will retrieve all of them, additionally, you can limit the amount of trips you want to return使用 Firestore 查询时,您可以在 trips 子项中进行直接复合查询并指定您想要的 UID,它将检索所有这些,此外,您可以限制要返回的行程数量

https://firebase.google.com/docs/firestore/query-data/queries https://firebase.google.com/docs/firestore/query-data/queries

With RTDB, you cannot make multiple filter queries at the same time hence why you would need to add a separate userTrips child in order to filter out all the trips to a specific user使用 RTDB,您不能同时进行多个过滤器查询,因此您需要添加一个单独的 userTrips 子项以过滤掉特定用户的所有行程

https://firebase.google.com/docs/database/ios/read-and-writehttps://firebase.google.com/docs/database/ios/read-and-write

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM