如何正确阻止代码执行直到回调

我是 Unity 和 C# 新手,并尝试查询我的 Firebase 实时数据库,但代码不会阻止回调完成。


我尝试过实现回调,但这似乎不起作用。


static public void ReadFromDb(int level, Action<int> callback) 

{

    int return_value = -1;

    string sessionId = PlayerPrefs.GetString("SessionID");


    FirebaseDatabase.DefaultInstance.GetReference("users/"+sessionId).GetValueAsync().ContinueWith(task => {

        if (task.IsFaulted) 

        {

            // Handle the error...

            Debug.Log("Task faulted");

            callback(return_value);

        } 

        else if (task.IsCompleted) 

        {

            DataSnapshot snapshot = task.Result;

            string score_string = (snapshot.Child(level.ToString()).Value).ToString();

            Debug.Log("score_string " + score_string);

            return_value = int.Parse(score_string);

            callback(return_value);

        }

    });

}



public void LevelComplete() 

{

    DatabaseCode.writeDatabase(SceneManager.GetActiveScene().buildIndex + 1, counter);

    DatabaseCode.ReadFromDb(SceneManager.GetActiveScene().buildIndex + 1, (result) => {

        prevlevelscore = result;

        Debug.Log("result " + result.ToString());

    });

    prevscore = prevlevelscore;

    Debug.Log("Returned value: " + prevlevelscore.ToString());

    SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);

}

在LevelComplete(),Debug.Log("Returned value: " + prevlevelscore.ToString());之前执行prevlevelscore = result;

,我想确保prevlevelscore在执行之前更新的值Debug.Log。


沧海一幻觉
浏览 95回答 2
2回答

慕桂英546537

将其余代码也放入回调中:public void LevelComplete()&nbsp;{&nbsp; &nbsp; DatabaseCode.writeDatabase(SceneManager.GetActiveScene().buildIndex + 1, counter);&nbsp; &nbsp; DatabaseCode.ReadFromDb(SceneManager.GetActiveScene().buildIndex + 1, (result) => {&nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("result " + result.ToString());&nbsp; &nbsp; &nbsp; &nbsp; prevscore = result;&nbsp; &nbsp; &nbsp; &nbsp; SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);&nbsp; &nbsp; });&nbsp; &nbsp;&nbsp;}

富国沪深

您的问题是您的 ReadFromDb 方法在完成之前返回。您可以通过将所有代码放入回调中来解决此问题(但您无法始终这样做),或者可以使用异步等待模式。使 ReadFromDb 异步:&nbsp; &nbsp; static public async Task ReadFromDb(int level, Action<int> callback)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int return_value = -1;&nbsp; &nbsp; &nbsp; &nbsp; string sessionId = PlayerPrefs.GetString("SessionID");&nbsp; &nbsp; &nbsp; &nbsp; await FirebaseDatabase.DefaultInstance.GetReference("users/"+sessionId).GetValueAsync().ContinueWith(task => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (task.IsFaulted)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Handle the error...&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Task faulted");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(return_value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if (task.IsCompleted)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DataSnapshot snapshot = task.Result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string score_string = (snapshot.Child(level.ToString()).Value).ToString();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("score_string " + score_string);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return_value = int.Parse(score_string);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; callback(return_value);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }请注意前面的等待键,GetValueAsync().ContinueWith因为此精确代码是异步的,如果您想在获取结果之前保留代码执行,则需要等待。在你的来电者中:&nbsp; &nbsp; public async Task LevelComplete()&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; DatabaseCode.writeDatabase(SceneManager.GetActiveScene().buildIndex + 1, counter);&nbsp; &nbsp; &nbsp; &nbsp; await DatabaseCode.ReadFromDb(SceneManager.GetActiveScene().buildIndex + 1, (result) => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; prevlevelscore = result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("result " + result.ToString());&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; prevscore = prevlevelscore;&nbsp; &nbsp; &nbsp; &nbsp; Debug.Log("Returned value: " + prevlevelscore.ToString());&nbsp; &nbsp; &nbsp; &nbsp; SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);&nbsp; &nbsp; }此方法也变为异步(因为异步性会传播)。同样,await 关键字将继续执行,直到 readFromDb 方法完成。这意味着您的数据将准备就绪。
打开App,查看更多内容
随时随地看视频慕课网APP