【发布时间】:2019-02-11 15:07:17
【问题描述】:
我在我的游戏中使用 firebase 实时数据库作为排名系统。 但是每次我尝试从 Datasnapshot 的孩子更改值的类型时,它似乎都脱离了 foreach
string cashierChild = "score_cashier";
FirebaseDatabase.DefaultInstance.GetReference("users").OrderByChild(cashierChild).LimitToLast(10)
.GetValueAsync().ContinueWith(task =>
{
if (task.IsFaulted)
{
Debug.Log("Failed To Load");
}
else if (task.IsCompleted)
{
int rank = 0;
DataSnapshot snapshot = task.Result;
foreach (var item in snapshot.Children)
{
rank = rank + 1;
Debug.Log("Rank: " + rank + ", nickname: " + item.Child("nickname").Value + ", score: " + item.Child(cashierChild).Value);
Debug.Log((int)item.Child(cashierChild).Value);
}
}
});
以上代码的结果
当我没有调用下面的代码时,它可以很好地打印出整个排名
Debug.Log((int)item.Child(cashierChild).Value);
没有代码
所以我虽然是因为类型转换,比如 (string) 或 (int)
【问题讨论】:
-
要真正比较它,您可能不得不使用
Debug.Log(item.Child(cashierChild).Value);,看看它会给您带来什么。另请注意,firebase 值的类型可以是string、long、bool、double... 没有int。尝试将不正确的值解析为int时,您可能会收到 Expection,尝试将过大的long值解析为int时,您可能会收到 OverflowException -
我不太确定我明白你在问什么。您可以在帖子中添加一个问题吗?另外:请显示您正在查询的 JSON(作为文本,请不要截图)。您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。
-
@derHugo @Frankvanpuffelen 我用其他方式转换类型解决了这个问题!!!
(string)Convert.ChangeType(item.Child("nickname").Value, typeof(string));谢谢你们的cmets!!
标签: c# firebase unity3d firebase-realtime-database