Firebase datadescription returns empty array

2019-08-28 12:23发布

I'm trying to Grab all Fields as dictionary from 1 document. But when I try to do it i just get returned with empty array. I tried getting help from documentation and it's not working.

tho this line of code shows it not empty

print("Cached document data: (dataDescription)")

var user:String = (Auth.auth().currentUser?.email)! // I used email as name of document.

var Groups = [String:Any]()

    let docRef = AppDelegate.db.collection("JoinedGroups").document(user)

    docRef.getDocument(source: .cache) { (document, error) in
        if let document = document {
            let dataDescription = document.data()
            self.Groups = dataDescription!   // unwrapping here
            print("Cached document data: \(dataDescription)")
        } else {
            print("Document does not exist in cache")
        }
    }
    // Do any additional setup after loading the view.
    print(Groups)

}

Here are my results: document.data() is shown as dictionary when i hoover over definition

[:] //Shown as empty??

Cached document data: Optional(["test new": test new, "g3": g3, "g1": g1])

Thank You very much if i can get some help on this issue.

1条回答
Anthone
2楼-- · 2019-08-28 13:02

The main issue here is that FireStore is asynchronous. It takes time for Firestore to return data from the internet and that data is only valid inside the closure following the getDocument.

That means the print(Groups) function will execute before the code inside the closure, so it's empty. Moving the print inside the closure will work.

var Groups = [String:Any]()
    let docRef = AppDelegate.db.collection("JoinedGroups").document(user)
    docRef.getDocument(source: .cache) { (document, error) in
        if let document = document {
            let dataDescription = document.data()
            self.Groups = dataDescription!   // unwrapping here
            print("Cached document data: \(dataDescription)")
        } else {
            print("Document does not exist in cache")
        }
        print(self.Groups)
    } 
}

May I also suggest you stick with naming conventions where vars are lowercases, e.g. groups instead of Groups. Upper case are usually for class definitions UserClass or GroupClass.

One last thing... documentID's (their 'key') cannot be changed. What that means is if your structure is this

JoinedGroups
   jimmy@email.com
      name: "Jimmy"
      fav_food: "Pizza"

and you reference that user throughout your app, when they decide to change their email provider, you'll have to go through everywhere in your strucutre, read in that node, delete the node and write it back out with the updated email. How you fix it is it disassociate the documentID (key) from the data it contains

JoinedGroups
   users_uid
      name: "Jimmy"
      fav_food: "Pizza"

as uid's never change, the email can change 20 times and not affect your app.

查看更多
登录 后发表回答