I’m having trouble getting an Async method to get a valid return. I’m using firestore and I need the return a variable that I can use in another script.
public async Task GetFromFirestore() //pulls data from firebase (used for buttons only).
{
Task<List<OrganizationTemplate>> t = GetOrg("Test Organization");
orgFromFS = await t;
Debug.Log(orgFromFS.Count);
//GetCart("Test Organization", "testCartID 1");
}
public async Task<List<OrganizationTemplate>> GetOrg(string _org)
{
List<OrganizationTemplate> tempOrgList = new List<OrganizationTemplate>();
//Asynchronous FireStore Portion
await firestoreInstance.Collection("organizations").Document(_org).GetSnapshotAsync().ContinueWithOnMainThread(getOrgs =>
{
Dictionary<string, object> tempOrg = getOrgs.Result.ToDictionary();
OrganizationTemplate o = new OrganizationTemplate(_org, tempOrg["name"].ToString(), tempOrg["icon"].ToString(), new List<CartTemplate>());
if(orgFromFS.Contains(o))
{
Debug.Log("Organization: " + o.ID + " already exists");
}
else
{
tempOrgList.Add(o);
Debug.Log("Organization: " + o.ID + "(" + o.Name + ") retrieved and added to list.");
}
});
//End of Async portion
Debug.Log(tempOrgList.Count + "Check 1");
return tempOrgList;
}
Since the Firestore portion of the code is async, I need to wait for it to complete before returning tempOrgList as the result of that. This is the first time I’m using an Asynchronous code and I read up on some examples on the Microsoft Task page. The code has no compilation errors.
When I run GetFromFirestore() I get a Unity Error: “ArgumentException: method return type is incompatible”.
If I remove all the Tasks and Awaits, the function works fine, but then I can’t get a valid return from GetOrg().
What am I overlooking or doing wrong? Thanks.
On which line? What’s the whole stack trace?
1 Like
It doesn’t say which line when I double click on the error, which is is why I can’t tell what I’m doing wrong. All I get is this:
ArgumentException: method return type is incompatible
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method, System.Boolean throwOnBindFailure, System.Boolean allowClosed) (at <9577ac7a62ef43179789031239ba8798>:0)
System.Delegate.CreateDelegate (System.Type type, System.Object firstArgument, System.Reflection.MethodInfo method) (at <9577ac7a62ef43179789031239ba8798>:0)
UnityEngine.Events.InvokableCall…ctor (System.Object target, System.Reflection.MethodInfo theFunction) (at :0)
UnityEngine.Events.PersistentCall.GetRuntimeCall (UnityEngine.Events.UnityEventBase theEvent) (at :0)
UnityEngine.Events.PersistentCallGroup.Initialize (UnityEngine.Events.InvokableCallList invokableList, UnityEngine.Events.UnityEventBase unityEventBase) (at :0)
UnityEngine.Events.UnityEventBase.RebuildPersistentCallsIfNeeded () (at :0)
UnityEngine.Events.UnityEventBase.PrepareInvoke () (at :0)
UnityEngine.Events.UnityEvent.Invoke () (at :0)
UnityEngine.UI.Button.Press () (at G:/Program Files/Unity/2019.4.12f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:68)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at G:/Program Files/Unity/2019.4.12f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/UI/Core/Button.cs:110)
UnityEngine.EventSystems.ExecuteEvents.Execute (UnityEngine.EventSystems.IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at G:/Program Files/Unity/2019.4.12f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:50)
UnityEngine.EventSystems.ExecuteEvents.Execute[T] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.ExecuteEvents+EventFunction`1[T1] functor) (at G:/Program Files/Unity/2019.4.12f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/ExecuteEvents.cs:261)
UnityEngine.EventSystems.EventSystem:Update() (at G:/Program Files/Unity/2019.4.12f1/Editor/Data/Resources/PackageManager/BuiltInPackages/com.unity.ugui/Runtime/EventSystem/EventSystem.cs:377)
Are you using Invoke to call this code?
Nope. Do I need to do that? I’m calling it like this:
- public async Task GetFromFirestore() //pulls data from firebase (used for buttons only).
- {
- Task<List> t = GetOrg(“Test Organization”);
- orgFromFS = await t;
- Debug.Log(orgFromFS.Count);
- //GetCart(“Test Organization”, “testCartID 1”);
- }
I’m just following the Asynch example on the Microsoft C# Tasks and Asynch pages.
That doesn’t look like how you’re calling it. It looks like how you’re defining it.
The error you’re getting seems to be from Unity’s Invoke system not seeing the method signature it expects. Are you invoking this code from a UnityEvent, for example the OnClick of a Button?
Yes. The GetFromFirestore() is called from a button.
Your error is basically saying that Unity can’t handle invoking a method that returns a Task from a button. Maybe invoke a different void method which in turn calls the async method and saves the returned task to a member variable.
I’ll give that a try, thanks.