'Coroutine not starting inside OnResponseGot

I've got a coroutine that should fetch an audioClip and then start another coroutine that plays that clip and waits until its finished so another clip could be played afterwards. The problem is - the program doesn't advance to the coroutine where I fetch the audioClip.

    private void RetrieveFromDatabase(int index)
    {
        Debug.Log($"{index} started...");
        FirebaseDatabase.DefaultInstance.GetReference("/Teams/" + TeamsSelection.teamSelected + "/").Child(index.ToString()).Child("userPosition").GetValueAsync()
           .ContinueWith(task => 
           {
               if (task.IsFaulted)
               {
                   //handle error
               }
               else if(task.IsCompleted)
               {
                   DataSnapshot snapshot = task.Result;
                   OnResponseGot(snapshot, index);
                   return;
               }
           });
    }

    private void OnResponseGot(DataSnapshot dataSnapshot, int index)
    {
        if (dataSnapshot == null || int.Parse(dataSnapshot.Value.ToString()) < 10)
        {
            Debug.Log("Trying again to retrieve " + index);
            RetrieveFromDatabase(index);
        }
        else
        {
            int userPos = int.Parse(dataSnapshot.Value.ToString());
            Debug.Log("retrieved userPos " + userPos);
            StartCoroutine(FetchWithUWR(userPos, index));
            //FetchWithResourceLoad(userPos, index);
        }
    }

    IEnumerator FetchWithUWR(int userPos, int index)
    {
        Debug.Log("Starting uwr for audioFiles");
        using (UnityWebRequest uwr = UnityWebRequestMultimedia.GetAudioClip($"C:/Users/Domas/Desktop/Projects/Wild_West/Assets/Resources/Audio/WAV_ENG/D{userPos}a", AudioType.WAV))
        {
            yield return uwr.SendWebRequest();
            Debug.Log("yielded a file!");
            if (uwr.isNetworkError)
            {
                Debug.Log(uwr.error);
            }
            else
            {
                AudioClip clip = DownloadHandlerAudioClip.GetContent(uwr);
                Debug.Log("Starting Coroutine " + index);
                StartCoroutine(PlayAudioClipAndStartRetrievingFromDatabase(index, clip));
            }
        }
    }

I don't even know where to start looking for a problem because I don't get any errors the program just keeps going but without the coroutine.



Solution 1:[1]

This problem can appear if you tried to start coroutine in other thread. In this case Firebase can handle exception and make it silent. Try use something like this to wait request in main thread:

    private bool requestDone;
    private DataSnapshot dataSnapshot;
    private int index;

    private void RetrieveFromDatabase(int index)
    {
        Debug.Log($"{index} started...");
        requestDone = false;
        StartCoroutine(WaitForResponce());

        FirebaseDatabase.DefaultInstance.GetReference("/Teams/" + TeamsSelection.teamSelected + "/").Child(index.ToString()).Child("userPosition").GetValueAsync()
           .ContinueWith(task => 
           {
               if (task.IsFaulted)
               {
                   //handle error
               }
               else if(task.IsCompleted)
               {
                   DataSnapshot snapshot = task.Result;
                   this.index = index;
                   thos.dataSnapshot = snapshot;
               }
               requestDone = true;
           });
    }

    private IEnumerator WaitForResponce()
    {
        while (!requestDone) yield return null;

        if (dataSnapshot == null || int.Parse(dataSnapshot.Value.ToString()) < 10)
        {
            Debug.Log("Trying again to retrieve " + index);
            RetrieveFromDatabase(index);
        }
        else
        {
            int userPos = int.Parse(dataSnapshot.Value.ToString());
            Debug.Log("retrieved userPos " + userPos);
            StartCoroutine(FetchWithUWR(userPos, index));
            //FetchWithResourceLoad(userPos, index);
        }
    }

Solution 2:[2]

I think there's the problem: you start the 'FetchWithUWR' coroutine inside the 'OnResponseGot' method. Also, you call 'OnResponseGot' inside the 'ContinueWith' event. You must start your coroutine in the main thread. When you use 'StartCoroutine' inside an 'Event' the coroutine starts on another thread. The following link helps you to find the solution: c# unity IEnumerator not working in eventhandler

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Radomyr Slaboshpitskyi
Solution 2 Abolfazl