Coming soon - Get a detailed view of why an account is flagged as spam!
view details

This post has been de-listed

It is no longer included in search results and normal feeds (front page, hot posts, subreddit posts, etc). It remains visible only via the author's post history.

2
Unable to store Firestore data into local variable
Post Body

Hi all,

I'm struggling with a simple task of saving my database data into a local variable. I've followed a previous project that did the exact same thing, but with my new project I can't seem to figure it out?

private var breweries: [Brewery] = []   

func loadDB(){
        db.collection("breweries").document("gq1amvMbkE6AlEHR4CxK")
            .addSnapshotListener { [self] (documentSnapshot, error) in
              guard let document = documentSnapshot else {
                print("Error fetching document: \(error!)")
                return
              }
              guard let data = document.data() else {
                print("Document data was empty.")
                return
              }

            for (key, value) in data{
                //Unwrap data here 
                //...
                //Save data to local variable
                self.breweries.append(newBrewery)
            }
        //Up until this point, data is saved to self.breweries
    }
  }

override func viewDidLoad() {
    //Do delegate stuff

    loadDB()

    //Here, self.breweries is empty.


}

I don't quite understand what's wrong here. In my other project (from a course), the same idea is applied where you append a new Object to a local variable. For some reason here it won't work?

Hoping someone can give me some insight on what I'm doing wrong, or what best practices would be in this scenario.

Thank you!

Author
User Disabled User Suspended
Account Strength
0%
Disabled 8 months ago
Suspended 9 months ago
Account Age
12 years
Verified Email
Yes
Verified Flair
No
Total Karma
55
Link Karma
5,091
Comment Karma
27,325
Profile updated: 13 hours ago
Posts updated: 10 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
2 years ago