Learn Courses My Dashboard

Structuring Firestore Data

Hey All,
I am creating a grocery list app. I have the ingredients structured in an array for each list on Firestore (see image). How would I go about structuring this so each ingredient can receive specific information (weight, color, etc.)? Should I add another subcollection? Thanks!

Hi! You can create map objects inside your array. They’re basically dictionaries that you can decode and encode into/from your model in Swift.

Thank you!

Hi, Andy and Cal!
Funny enough I am working on a similar project and am wondering how you went about importing the data from the solution you just posted. Every time I try to read my items field it comes up empty. items is structured the same way (it is an array of map objects).

This is how I am currently trying to fetch the data from firebase.

func getAtHomeData() {
// Specify collection path
let listCollection = db.collection(“atHome”)

    // Get Documents
    listCollection.getDocuments { snapshot, error in
        // Check for nils
        if error == nil && snapshot != nil {
            // Array to track categories
            var categories = [Category]()
            for doc in snapshot!.documents {
                var c = Category()
                c.id = doc["id"] as? String ?? UUID().uuidString
                c.category = doc["Category"] as? String ?? ""
                c.items = doc["items"] as? [Item] ?? [Item]()

            // Assign our categories array to published atHome variable
            DispatchQueue.main.async {
                self.atHome = categories
    // Specify item Collection path

And these are my structures:

struct Category: Identifiable {

var id: String = “”

var category: String = “”

var items = Item


struct Item: Identifiable {

var id: String = “”

var name: String = “”

var quantity: Int = 0


And my database looks like

Hey Mullen,

I am actually still struggling reading the data. I believe you have to iterate over the “item” array, but I have not gotten the syntax correct.