Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Coroutine doesn't terminate with the callback.

Discussion in 'Scripting' started by Sushi8819, Nov 17, 2023.

  1. Sushi8819

    Sushi8819

    Joined:
    Oct 20, 2023
    Posts:
    17
    I want to return a string as the return value of the coroutine, but even when calling the callback, the process doesn't get interrupted. Is this because I'm writing my code incorrectly? Can you provide any guidance?
    Thank you !!

    Code (CSharp):
    1.  
    2.     private IEnumerator UploadImageToCloud(string image_dir, Action<string> callback)
    3.     {
    4.         string imageUrl = "";
    5.  
    6.         byte[] bytes = File.ReadAllBytes(dir)
    7.             ;
    8.         Texture2D texture = new Texture2D(2, 2);
    9.         texture.LoadImage(bytes);
    10.  
    11.         if (texture != null)
    12.         {
    13.             int width = texture.width;
    14.             int height = texture.height;
    15.  
    16. // I got the correct size here.
    17.             Debug.Log($"Size : {width} / {height}");
    18.  
    19.             if (width!=TARGET_SKIN_WIDTH || height!=TARGET_SKIN_HEIGHT)
    20.             {
    21.                 Debug.Log("brefore_callback");
    22.                 callback(imageUrl);
    23.                 Debug.Log("after_callback");
    24.  
    25. // I got both of "brefore" and "after" _callback logs.
    26. // I want to return "", if the texture size does't match the target size.
    27.             }
    28.         }
    29.         else
    30.         {
    31.             callback(imageUrl);
    32.         }
    33.  
    34.        byte[] bytes = File.ReadAllBytes(dir);
    35.  
    36.         yield return StartCoroutine(DoingPutRequest(bytes,  x => imageUrl = x));
    37.  
    38.         callback(imageUrl);
    39. }
    40.  
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,295
    Generally, you don't want to "return" anything from a Coroutine. The whole point is that the Coroutine will continue to run on future frames, so there's nobody left in the first frame to receive your final results.

    You are mixing two forms of starting a Coroutine (DoingPutRequest) from within another Coroutine (UploadImageToCloud). You should choose one or the other of these forms:

    StartCoroutine(DoingPutRequest(...));

    This will start another coroutine along with the first one. Both run in parallel until they finish independently.

    yield return DoingPutRequest(...);

    This will make the outer coroutine wait for the inner coroutine to complete before continuing.

    If you need the results from a long operation, you should be using some form of async/await scheme, instead of Coroutines.
     
    spiney199 likes this.
  3. Sushi8819

    Sushi8819

    Joined:
    Oct 20, 2023
    Posts:
    17
    I've been avoiding Unitask because it seemed difficult to handle, but it might be time to start studying.
    Thank you.
     
  4. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,857
    I'm confused what your actual problem is. Strangely you call your callback at 3 different places but the first 2 calls would pass an empty string to the coroutine. You said:
    Do you mean you want to stop the coroutine when you call your callback? If that's the case you have to put a
    yield break;
    after your callback call. When you call your callback it's just an ordinary method call. When the callback has finished, the coroutine would continue unless you break out of it. How / from where do you actually call / start your coroutine? And how does your callback look like?

    ps: This piece of code:
    Code (CSharp):
    1.  x => imageUrl = x
    is actually redundant. You could simply pass in your "callback" instead. You create a closure that essentially will provide you a method with the same signature as your callback (so it just gets a single string argument) but you route it through your local imageUrl string just to pass it to your callback. So you can simply do:



    Code (CSharp):
    1. yield return DoingPutRequest(bytes,  callback);
    So your inner coroutine DoingPutRequest would directly call your callback when it has the necessary data.
     
  5. Sushi8819

    Sushi8819

    Joined:
    Oct 20, 2023
    Posts:
    17
    Bunny83
    Thank you for the response!
    I had always thought that a coroutine would be interrupted as soon as callback() was called , like 'yield break' you said.
    This might be the cause of the issue.
    I might have to check all the code I've written so far...