本文介绍了Firebase Unity - 在GetValueAsync之后将所有的孩子都放到一个数组/通用列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Unity3D的Firebase数据库中以数组或通用列表的形式获取数据,而不提前知道孩子的名字(键)是什么?


$ b

我一直在尝试新的 Unity Firebase插件,我有一个问题弄清楚如何让所有的孩子在一个特定的位置,并把名称(关键)和价值数组或通用名单,使我可以在本地处理数据。请原谅我对Firebase来说是如此的陌生,也许使用了不好的技术来做到这一点,而且这个插件非常新,对我来说很难获得很多的外部帮助,因为Firebase Unity上没有很多文档和教程。

在这种特殊情况下,我尝试创建即时消息功能,不使用Firebase消息功能,仅使用常规的Firebase数据库的东西。使用Firebase消息可能会更加容易,但主要是为了学习和自定义,我只想使用Firebase数据库自行完成此操作。



我插入数据到数据库是这样的:

pre $ public $ SendMessage(string toUser,string msg)
{
Debug.Log(String.Format(试图从{0}发送消息到{1},用户名,toUser));

DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference(Msgs);
string date = Magnet.M.GetCurrentDate();

//将数据发送到DB
reference.Child(toUser).Child(username).Child(date).SetValueAsync(msg);
//用户接收消息/用户发送消息> VALUE =hello dude | 20170119111325

UpdateUsers();
}

然后我试着恢复它:

  public string [] GetConversation(string userA,string userB)
{
//获取两个用户之间的对话
string [] convo = new string [0];

FirebaseDatabase.DefaultInstance.GetReference(Msgs)。GetValueAsync()。ContinueWith(task =>
{
Debug.Log(Getting Conversation ...) ;

if(task.IsFaulted || task.IsCanceled)
{
Debug.LogError(错误:GetConversation():+ task.Exception)中的任务错误;

else if(task.IsCompleted)
{
DataSnapshot snapshot = task.Result;

string [] messagesA = new string [0] ,messagesB = new string [0];

if(snapshot.HasChild(userA))
{
// userA有与其他用户对话的记录
$ if $ {

Debug.Log(找到childA);
long count = snapshot.Child(userA).Child(userB).ChildrenCount;
messagesA =新字符串[count];
var kids = snapshot.Child(userA).Child(userB).Children;
Debug.Log(kids);
for(int i = 0; i< count; i ++)
{
//这将不起作用,但是我想如何访问数据
messagesA [i] = kids [i] .Value.ToString();如果(snapshot.HasChild(userB))
{$ AGAIN .... will not work ...
}
}
}


if(snapshot.Child(userB).HasChild(userA))// userA在
{
Debug.Log(Found childB)之前向userB发送了一条消息。
long count = snapshot.Child(userB).Child(userA).ChildrenCount;
messagesA =新字符串[count];
var kids = snapshot.Child(userB).Child(userA).Children;
Debug.Log(kids);
//凌乱的不完整的测试代码...
}
}

//这里我将分配A和B之间的所有消息作为'convo'.. 。
}

Debug.Log(完成对话);
});


返回convo;





$ b

但显然这是行不通的,因为DataSnapshot不会让我存取它像一个数组或通用列表使用索引,我不知道如何处理数据,当我不知道所有的孩子的名字(钥匙),只是想把他们一个接一个地在任何顺序...而且由于它们是按日期/时间命名的,所以我不会提前知道孩子的名字(键)是什么,我不能只说GetChild(

仅供参考,数据库看起来像: b


解决方案

Firebase开发人员在这里。



您是否曾尝试在顶级快照中使用Value?它应该返回给你一个IDictionary,其中的值也可以是列表或嵌套字典。你将不得不使用一些动态检查来找出这些值是什么。


How can I get data back as an array or generic list from a Firebase database in Unity3D without knowing ahead of time what the name (key) of the children are?

I have been trying out the new Unity Firebase plugin, and I am having an issue figuring out how to get all the children in a specific location, and put the names (the key) and the values into arrays or generic lists so that I can work on the data locally. Forgive me for being so new to Firebase and probably using bad techniques to do this, and this plugin being so new its pretty hard for me to get much outside help, as there are not a lot of docs and tutorials out there on Firebase Unity.

In this particular case I am trying to create "instant messaging" like functionality, without the use of Firebase messaging, and just using regular Firebase database stuff instead. It might have been easier to use Firebase messaging, but mostly for the sake of learning and customization I want to do this on my own with just the Firebase database.

I insert data into the database like this:

public void SendMessage(string toUser, string msg)
{
    Debug.Log(String.Format("Attempting to send message from {0} to {1}", username, toUser));

    DatabaseReference reference = FirebaseDatabase.DefaultInstance.GetReference("Msgs");
    string date = Magnet.M.GetCurrentDate();

    // send data to the DB
    reference.Child(toUser).Child(username).Child(date).SetValueAsync(msg);
    // user receiving message / user sending message > VALUE = "hello dude|20170119111325"

    UpdateUsers();
}

And then I try and get it back like this:

public string[] GetConversation(string userA, string userB)
{
    // get a conversation between two users
    string[] convo = new string[0];

    FirebaseDatabase.DefaultInstance.GetReference("Msgs").GetValueAsync().ContinueWith(task =>
    {
        Debug.Log("Getting Conversation...");

        if (task.IsFaulted || task.IsCanceled)
        {
            Debug.LogError("ERROR: Task error in GetConversation(): " + task.Exception);
        }
        else if (task.IsCompleted)
        {
            DataSnapshot snapshot = task.Result;

            string[] messagesA = new string[0], messagesB = new string[0];

            if(snapshot.HasChild(userA))
            {
                // userA has a record of a conversation with other users
                if(snapshot.Child(userA).HasChild(userB)) // userB has sent messages to userA before
                {
                    Debug.Log("Found childA");
                    long count = snapshot.Child(userA).Child(userB).ChildrenCount;
                    messagesA = new string[count];
                    var kids = snapshot.Child(userA).Child(userB).Children;
                    Debug.Log(kids);
                    for (int i = 0; i < count; i++)
                    {
                        // this won't work, but is how I would like to access the data
                        messagesA[i] = kids[i].Value.ToString(); // AGAIN.... will not work...
                    }
                }
            }

            if(snapshot.HasChild(userB))
            {
                if(snapshot.Child(userB).HasChild(userA)) // userA sent a message to userB before
                {
                    Debug.Log("Found childB");
                    long count = snapshot.Child(userB).Child(userA).ChildrenCount;
                    messagesA = new string[count];
                    var kids = snapshot.Child(userB).Child(userA).Children;
                    Debug.Log(kids);
                    // messy incomplete testing code...
                }
            }

            // HERE I WOULD ASSIGN ALL THE MESSAGES BETWEEN A AND B AS 'convo'...
        }

        Debug.Log("Done Getting Conversation.");
    });


    return convo;
}

But obviously this won't work, because DataSnapshot won't let me access it like an array or generic list using indices, and I can't figure out how to treat the data when I don't know the names (the keys) of all the children, and just want to get them out one by one in any order... and since they are named by the date/time they are entered into the DB, I won't know ahead of time what the childrens names (keys) are, and I can't just say "GetChild("20170101010101")" because that number is generated when its sent to the DB from any client.

FYI here is what the DB looks like:

解决方案

Firebase developer here.

Have you tried to use Value at the top level Snapshot? It should return to you an IDictionary where the values can also be lists or nested dictionaries. You will have to use some dynamic inspection to figure out what the values are.

这篇关于Firebase Unity - 在GetValueAsync之后将所有的孩子都放到一个数组/通用列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 17:28