iosswiftfirebase

How to Structure a Firebase Database and Query Documents from District in Swift?


I am developing an app using SwiftUI and Swift. I have a Firebase database with the following structure:

Collection named products, Documents representing countries, Subcollections representing districts, Documents representing cities, Subcollections containing various documents. Given this structure, how can I retrieve all documents from a specific district, noting that a district contains multiple cities, each with its own set of documents?

Any code examples or explanations on how to achieve this in Swift would be greatly appreciated.


Solution

  • Subcollections representing districts

    how can I retrieve all documents from a specific district?

    This would require that you can get a list of subcollections under a given path, which is not an operation that Firestore supports.

    Given that, you'll have to (also) store a list of all the districts as data in another document - one of which you know the name. For example, it's pretty common to include this info in an array field in the parent document of the districts collections - but other options are fine too.