neuecc / UniRx

Reactive Extensions for Unity
MIT License
7.08k stars 891 forks source link

Observable from UnityWebRequest calls OnComplete although status is still InProgress #530

Open achimmihca opened 1 year ago

achimmihca commented 1 year ago

I do the following to observe a UnityWebRequest:

UnityWebRequest unityWebRequest = UnityWebRequest.Get(uri);

void OnNext()
{
    Debug.Log($"{unityWebRequest.method} '{unityWebRequest.uri}' has updated. Status: {unityWebRequest.result}, response code: {unityWebRequest.responseCode}");
}

void OnError
{
    Debug.LogError($"{unityWebRequest.method} '{unityWebRequest.uri}' has failed. Status: {unityWebRequest.result}, response code: {unityWebRequest.responseCode}, error message: {ex.Message}");
    Debug.LogException(ex);
}

void OnComplete()
{
    Debug.Log($"{unityWebRequest.method} '{unityWebRequest.uri}' has completed. Status: {unityWebRequest.result}, response code: {unityWebRequest.responseCode}, response body: {unityWebRequest.downloadHandler.text}");
}

unityWebRequest
    .SendWebRequest()
    .AsAsyncOperationObservable()
    .Subscribe(OnSucess, OnError, OnComplete);

My problem is that sometimes (not always) the OnComplete method is called although unityWebRequest.result is still InProgress, the unityWebRequest.downloadHandler.text is empty, and unityWebRequest.isDone is false. Thus, OnComplete seems to be called prematurely sometimes.

Is this a bug? Or do I have to create the Observable from the UnityWebRequest differently somehow?

achimmihca commented 1 year ago

I noticed that there are

I think these would be obsolete if the above example would work as expected (i.e. OnComplete was not called prematurely)