Trouble waiting for async method call - Coroutines

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:

public IEnumerator LinkAndStart(){

ParseScript._parseInstance.GetUserObjectId(nameInputField.text, Convert.ToInt32(pinInputField.text), pinInputField.text);

//I need to wait until the above completes before loading the next scene
StartCoroutine(LoadScene());
.....
}

Here is the func that contains a Parse sdk aync query

public void GetUserObjectId(string username , int pin, string pinnumber)
{
var query = ParseObject.GetQuery("UniqueID")
        //var query = ParseObject.GetQuery("Users")
            .WhereEqualTo("PIN", pinnumber);
        //Task queryTask = query.FindAsync().ContinueWith(t => {
            query.FindAsync().ContinueWith(t => {
            IEnumerable<ParseObject> results = t.Result;
     .......  I want to return a success(true)  or failure (false) based on the result of this query
             

}

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?

Hi there!

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

using System;

…

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

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

public class XXX {

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

}

Cheers!
Booil.

Normally you just wait until the task is done. Here is one way to do it.

IEnumerator LongTask (){
    var task = StartLongTask();
    while (!task.isDone) yield return null;
    DoNextJob();
}
2 Likes

ah excellent I will try this, makes a lot more sense. Thank you! Will report back. -Ethan