Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trouble waiting for async method call - Coroutines

Discussion in 'Scripting' started by ethandrower, Mar 7, 2017.

  1. ethandrower

    ethandrower

    Joined:
    Mar 6, 2017
    Posts:
    2
    Hi all,
    I've done a good amount of reading on coroutines and have used c# async await logic before but can't seem to get either to work in the context of a unity project.

    Here is my calling function:


    Code (CSharp):
    1. public IEnumerator LinkAndStart(){
    2.  
    3. ParseScript._parseInstance.GetUserObjectId(nameInputField.text, Convert.ToInt32(pinInputField.text), pinInputField.text);
    4.  
    5. //I need to wait until the above completes before loading the next scene
    6. StartCoroutine(LoadScene());
    7. .....
    8. }

    Here is the func that contains a Parse sdk aync query

    Code (CSharp):
    1. public void GetUserObjectId(string username , int pin, string pinnumber)
    2. {
    3. var query = ParseObject.GetQuery("UniqueID")
    4.         //var query = ParseObject.GetQuery("Users")
    5.             .WhereEqualTo("PIN", pinnumber);
    6.         //Task queryTask = query.FindAsync().ContinueWith(t => {
    7.             query.FindAsync().ContinueWith(t => {
    8.             IEnumerable<ParseObject> results = t.Result;
    9.      .......  I want to return a success(true)  or failure (false) based on the result of this query
    10.              
    11.  
    12. }
    Now I've tried calling the GetUserObjecId as a Coroutine, but I get an error because you cannot put yield returns inside anonymous blocks (the parse sdk query).

    I guess I'm just a little confused how I can wait for this function call to complete and return a status to the caller. Running how it is now, I'd assume that unity would wait until the called method returns, but breakpoints i have in the return statements of the query never get hit.

    Any ideas?
     
  2. booiljoung

    booiljoung

    Joined:
    May 12, 2013
    Posts:
    57
    Hi there!

    Coroutines can't return result values.
    Lambda function can be solution for your problem.

    using System;

    ...

    Action<bool> resultFunc = (bool result) => {
    if (result) {
    ....
    }
    };

    yield return this.StartCoroutine(XXX.Query(resultFunc));



    public class XXX {

    public static Enumerator Query(Action<bool> resultFunc>) {
    yield return ....
    ....
    resutlFunc(true); // or false
    }

    }

    Cheers!
    Booil.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Normally you just wait until the task is done. Here is one way to do it.

    Code (CSharp):
    1. IEnumerator LongTask (){
    2.     var task = StartLongTask();
    3.     while (!task.isDone) yield return null;
    4.     DoNextJob();
    5. }
     
    EDevJogos and lauraaa like this.
  4. ethandrower

    ethandrower

    Joined:
    Mar 6, 2017
    Posts:
    2
    ah excellent I will try this, makes a lot more sense. Thank you! Will report back. -Ethan