简体   繁体   中英

How to use async in Unity with Firebase

So I'm trying to use Firebase in my Unity project, I've never done anything with async related stuff and I'm not understanding how to work around this issue.

public DatabaseData GetData()
{
    DatabaseData data = new DatabaseData();
    reference.GetValueAsync().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Debug.Log("error");
        }
        else if (task.IsCompleted)
        {
            DataSnapshot snapshot = task.Result;
            // Do something with snapshot...
            Debug.Log("hit");
        }
    });
    return data;
}

So I've tried following what's going on in the function, and noticed that it returns before it has hit the Debug.Log("hit") which results in having an empty variable returned.
I tried searching around and seeing people use coroutines but that won't let me return any data.

Try declaring the reference before such as private DatabaseReference d;

d = ...GetReference("your refernece"); simple call d.child("yourchild").GetValueAsync()...

Yes it returns the empty data because the task hasn't finished. You're not waiting for it to finish - which would defeat the point of using async in the first place. The standard answer would be to use async/await if you can, but don't touch any Unity stuff in the continuation because it will run on a pool thread. Edit: Actually Unity defines a synchronization context meaning you don't have to worry about being in another thread after await: http://www.stevevermeulen.com/index.php/2017/09/using-async-await-in-unity3d-2017/

I know i am late to the party, but in case anyone else stumbles on this like I did:

Thank you to @Jesper for the link, it was very helpful, and I encourage everyone to read it, however if you are looking for a quick fix, here is how I would rewrite the above example using the info I gained from Jesper's link.

public async Task GetData()
{
    DatabaseData data = new DatabaseData();
    await reference.GetValueAsync().ContinueWith(task =>
    {
        if (task.IsFaulted)
        {
            Debug.Log("error");
        }
        else if (task.IsCompleted)
        {
            DataSnapshot snapshot = task.Result;
            // Do something with snapshot...
            Debug.Log("hit");
            GLOBAL_VAR_DATA = data;
        }
    });
    return;
}

This will cause the the program to halt and wait for getValueAsync to complete its task before continuing. Also, it it now returns the task from getValueAsync instead of data. That will have to be stored globally and retrieved elsewhere. You would use it like this:

await GetData();
data = GLOBAL_VAR_DATA;

Await GetData will completely resolve, then it will pull the data from the global variable. If it was successful, GLOBAL_VAR_DATA has the data you want, and can be retrieved at this time.

https://blog.xuite.net/akira32/home/590700144

Unity firebase async and await when I use dropdown RefreshShownValue

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM