亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確阻止代碼執行直到回調

如何正確阻止代碼執行直到回調

C#
滄海一幻覺 2023-09-16 17:27:24
我是 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。
查看完整描述

2 回答

?
慕桂英546537

TA貢獻1848條經驗 獲得超10個贊

將其余代碼也放入回調中:


public void LevelComplete() 

{

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

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

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

        prevscore = result;

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

    });    

}


查看完整回答
反對 回復 2023-09-16
?
富國滬深

TA貢獻1790條經驗 獲得超9個贊

您的問題是您的 ReadFromDb 方法在完成之前返回。您可以通過將所有代碼放入回調中來解決此問題(但您無法始終這樣做),或者可以使用異步等待模式。


使 ReadFromDb 異步:


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

    {

        int return_value = -1;

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


        await 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);

            }

        });

    }

請注意前面的等待鍵,GetValueAsync().ContinueWith因為此精確代碼是異步的,如果您想在獲取結果之前保留代碼執行,則需要等待。


在你的來電者中:


    public async Task LevelComplete() 

    {

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

        await 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);

    }

此方法也變為異步(因為異步性會傳播)。同樣,await 關鍵字將繼續執行,直到 readFromDb 方法完成。這意味著您的數據將準備就緒。


查看完整回答
反對 回復 2023-09-16
  • 2 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號