简体   繁体   中英

Loading a dictionary of lists of lists in Unity with Firebase

So im saving my Dictionary like this:

public void saveData()
{
    database.Child("Users").Child("User").SetValueAsync(shoppingList);    
}

But if I want to load my dictionary it always returns null when I want to access anything. I load it like this:

    public void loadData()
    {

    FirebaseDatabase.DefaultInstance.GetReference("Users").GetValueAsync().ContinueWith(task => {
        if (task.IsFaulted)
        {
            // Handle the error...
        }
        else if (task.IsCompleted)
        {
            var result = task.Result;

            var loadedDict = result.Value as Dictionary<string, List<List<string>>>;

            Debug.Log(loadedDict);       

            
        }
    });
}

I think what might be happening is that the var loadedDict = result.Value as Dictionary<string, List<List<string>>>; is not making the conversion rightaway.

I would check the following.

1.- That the data is being properly saved in the save stage, and stored in the database and confirm that in the question.

2.- Even the dictionary is null, check if the data returned from the task is null. Because the data might be being retrieved in the var result = task.Result;, but the conversion to the dictionary not working. I would also confirm that in the question if that is the case.

3.- If the data is actually being retrieved in stage 2, and what is not working is the conversion. As far as the documentation says, the values returned native types are:

bool , string , long , double , IDictionary{string, object} and List{object}

If you are receiving the data but the loadedDict is null, you will need to code the method that converts the IDictionary{string, object} to your Dictionary<string, List<List<string>>> with your info filled in.

I did not use firebase with unity, only in web projects, so I am not sure if this might by your problem, is just my guess. I'll be glad to check what you find out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM