如何使用C#检查Firebase快照节点是否存在?

时间:2019-01-02 20:01:23

标签: c# firebase unity3d firebase-realtime-database

我正在使用Unity应用程序,并使用C#使用Google Firebase。一方面,我必须编辑现有信息或添加新信息。为此,我必须检查快照中是否存在节点,但是找不到找到该节点的函数。

HasChild()始终返回true,因此没有帮助。我已经搜索过,并且函数exist()总是被提及为最佳选择,但是当我尝试使用它(写为“ exists”或“ Exists”)时,出现以下错误: error CS1061: Type 'Firebase.Database.DataSnapshot' does not contain a definition for 'Exists' and no extension method 'Exists' of type 'Firebase.Database.DataSnapshot' could be found. Are you missing an assembly reference?(...) does not contain a definition for 'exists' (...)

// The data structure looks like:
// .../questions
//      question_0
//          index = 0
//          other information...
//      question_2
//          index = 3
//          other information...
//      question_3
//          index = 5
//          other information...
//      question_6
//          index = 4
//          other information...

// The code I'm using:

public static DataSnapshot dbSnapshot; // I get this from an Event Listener

public void MyMethod(){
    int i = 0;
    bool done = false;
    do {
        string path = "questions/question_" + i.ToString() + "/index";
            if (dbSnapshot.Child(path).Exist()){  // The line that does not work!
            long newValue = (long) dbSnapshot.Child(path).Value;
            // If true, I'll do something here with this value
        } else {
            // If not, I'll do something else here
        }


        // at some point later "done" becomes true, ending loop
        // this part of the code works just fine
    } while (!done);
}

无法使用Exist(),我尝试检查空值:

if (dbSnapshot.Child(path).Value != null)

但是,每当遇到不存在的question_i时,我都会得到一个错误:InvalidCastException: Specified cast is not valid.在if-satament本身内不会发生此错误,而是在下一行。看起来... Value!= null返回true,因此在下一行应用程序尝试获取该值,但由于该值不存在,因此出现错误。

那么,如何在不查找空值或不使用Exist()和HasChild()函数的情况下检查节点是否存在?

0 个答案:

没有答案
相关问题