简体   繁体   中英

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).

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. 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.

Not sure if it will make a difference but I am using 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
      • firstName: "James"
      • email: "a@email.com"
    • SecondUID
      • firstName: "John"
      • email: "b@email.com"
  • trips:

    • FirstTripId
      • startDate: "2/5/2022"
      • destination: "Orlando, FL"
      • uid: FirstUID
      • food: "150"
      • hotel: "400"
      • fuel: "250"
      • misc: "75"
    • SecondTripId
      • startDate: "3/5/2022"
      • destination: "Harare, Zimbabwe"
      • uid: SecondUID
      • food: "21"
      • hotel: "69"
      • fuel: "420"
      • misc: "7"

/// add this next part if you wish to do less queries or if you ever use rtdb

  • userTrips
    • FirstUID
      • FirstTripId
        • startDate: "2/5/2022"
      • NextTripIds
    • SecondUID
      • SecondTripId
        • startDate: "3/5/2022"

///

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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