【发布时间】:2020-02-19 18:43:09
【问题描述】:
我有一个使用 Firebase 进行 Google 身份验证的 Unity 项目。我将用户信息存储在实时数据库中。它们存储为:
UnityProject
1->
Name: "Something"
Email: "Something"
2->
Name: "some other thing"
Email: "Something else"
现在,这 1,2 将由我给出(用作主键)。我将从给第一个用户 ID 1 和第二个使用的 ID 2 开始,依此类推。
但我需要从 Firebase 取回存储的最后一个 ID。例如,
idToInsert = GetLastUsedID() + 1;
我已使用此代码,但它不起作用。屏幕会冻结,直到我强制关闭 Unity。
public int GetLastUsedID()
{
int currentID = 1;
bool continueSearch = true;
while (continueSearch)
{
FirebaseREST.DatabaseReference reference = FirebaseREST.FirebaseDatabase.Instance.GetReference(""+currentID);
string value = "";
currentID++;
reference.GetValueAsync(10, (res) =>
{
if (res.success)
{
value = res.data.GetRawJsonValue();
Debug.Log("Success fetched data : " + value);
if(value == "")
{
continueSearch = false;
Debug.Log(currentID);
}
}
else
{
Debug.Log("Fetch data failed : " + res.message);
continueSearch = false;
}
});
}
return currentID;
}
基本上,我只是想从 1 迭代直到我得到空字符串。空字符串表示该 ID 下不存在数据。
【问题讨论】:
-
好好看看你的函数,尤其是while循环和.GetValueAsync,因为我不知道这是否是你想要实现的。 GetValueAsync 顾名思义,它的工作是异步的,第二个它被称为你的 while 循环的主体再次开始工作。我认为这不是你的意图,对吗?
-
我只想获取数据库中的最后一个数字。在函数中获取 id1 的值。如果返回空字符串,则停止整个循环...
标签: c# firebase unity3d firebase-realtime-database