Jamshaid Alam
Jamshaid Alam

Reputation: 527

How to read multiple custom objects from Firestore in Unity 3D?

So I was struggling in past couple of days to get multiple custom objects from Firestore. There are no examples to get multiple custom objects in Firestore Docs.

So, here is what I want. I want to populate list of user who is already in my collection with some limits and orders. I was able to populate using KeyValuePair but don't know how to get all values from collection. Here is my code.

public void GetAllDocs()
    {
        Query allUsersQuery = db.Collection ("users").OrderByDescending ("counter").Limit (11);
        allUsersQuery.GetSnapshotAsync ().ContinueWithOnMainThread (task =>
        {
            QuerySnapshot allUsersQuerySnapshot = task.Result;
            foreach (DocumentSnapshot documentSnapshot in allUsersQuerySnapshot.Documents) {
                Debug.Log (String.Format ("Document data for {0} document:", documentSnapshot.Id));
                Dictionary<string, object> user = documentSnapshot.ToDictionary ();
                foreach (KeyValuePair<string, object> pair in user) {
                    Debug.Log (String.Format ("{0}: {1}", pair.Key, pair.Value));
                    
                    if (pair.Key == "username") {

                        GameObject tBar = Instantiate (TrendingUser.gameObject, transform.position, transform.rotation);
                        tBar.transform.SetParent (TrendingUserParent, false);
                        tBar.transform.GetChild (1).GetComponent<TMP_Text> ().text = (string)pair.Value;
                        tBar.gameObject.name = (string)pair.Value;
                        RectTransform myRectTransform = tBar.gameObject.GetComponent<RectTransform> ();
                        myRectTransform.localPosition = new Vector3(myRectTransform.position.x, myRectTransform.position.y, 0);
                    }
                }
            }
        });
    }

Now, if I want more data like ImageURL, Location etc which is in same collection how will I get that? Do I need to use Custom Objects or is there any way to do this using KeyValuePair itself? Please help me. Thanks

Upvotes: 1

Views: 630

Answers (1)

Happy-Monad
Happy-Monad

Reputation: 2002

From what I understand you're retrieving a couple of users (i.e user documents) from a Firestore collection and you need to cast the results to Unity objects.

In general, it is up to you how to model your data domain be it with classes or dictionaries, there's no reason preventing you to retrieve/cast multiple attributes from a document. However, I see the code iterates across the documents and creates a GameObject for each document once you find the username key. This way it's not possible to add other keys since you may have just skipped them while iterating through other document properties.

Then, if you want to keep using dictionaries I would suggest something along these lines:

Dictionary<string, object> user = documentSnapshot.ToDictionary ();
if (user.ContainsKey("username")) {
    // Instantiate your GameObject here
    // Then read any other properties needed
}

Upvotes: 1

Related Questions