How to return a value from a anonymous function?,
I'm trying to have a method that reads something from Firebase Database then returns the Datasnapshot as output. I have the following code:
public DataSnapshot getElements() {
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://somedatabaseURL.com/");
FirebaseDatabase.DefaultInstance.RootReference.GetValueAsync().ContinueWith(task => {
if (task.IsFaulted)
{
Debug.Log("not ok");
}
else if (task.IsCompleted)
{
DataSnapshot snapshot = task.Result;
return snapshot
}
});
}
Getting the following error: "Anonymous function converted to a void returning delegate cannot return a value."
Searched the answer on all over the internet, couldnt find anything. How to overcome it?,
Answer by Hellium · Jul 30, 2018 at 05:02 AM
What you are trying to do is impossible. You can't expect a synchronous function to directly return something retrieved by an asynchronous function.
You will need to provide a callback to your synchronous function that will be called once your asynchronous function has terminated.
public void getElements( System.Action<DataSnapshot> onSnapshotRetrieved ) {
FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://somedatabaseURL.com/");
FirebaseDatabase.DefaultInstance.RootReference.GetValueAsync().ContinueWith(task => {
if (task.IsFaulted)
{
Debug.Log("not ok");
}
else if (task.IsCompleted)
{
onSnapshotRetrieved ( task.Result ) ;
}
});
}
// ...
getElements ( snapshot => Debug.Log( "Snapshot retrieved!" ) ) ;
Answer by adrenalmedulla · Jul 31, 2018 at 05:16 PM
Hi again @Hellium ,
I have tried your solution but that doesn't solve my problem since System.Action doesn't let me return a value. System.Func is also not working, since it doesn't accept any input.
public void Load(string assetName)
{
Debug.Log("1. flag");
getElements(snapsnapshot => { Debug.Log(snapsnapshot.ChildrenCount); });
Debug.Log("2. flag");
}
When I run the above snippet with the same getElements method you described, following is printed:
'1. flag'
'2. flag'
'3' (Number of Children)
However, I need it to be like:
'1. flag'
'3' (Number of Children)
'2. flag'
Otherwise it returns a value from the method before assigning the new value. Do you have any comment to this situation?
As I said, you can't expect the execution of an asynchronous method to be completed before the end of the synchronous method that called it.
The best you can do is:
public void Load(string assetName)
{
getElements(snapsnapshot =>
{
Debug.Log("1. flag");
Debug.Log(snapsnapshot.ChildrenCount); });
Debug.Log("2. flag");
}
}
To be more precise what I really want is to run this code snipped as it is sync. I know, I cannot assign 'datasnapshot' like that but I'm trying to achieve that with any possible solution.
public Datasnapshot Load(string assetName)
{
Datasnapshot datasnapshot = getElements(datasnapshot => {
return datasnapshot; });
return datasnapshot;
}
Thanks for your interest
Supposing you are invited to a party with your friend. You are asked to bring a pizza. You call your favourite pizzaiolo to make you the pizza. Do you think you can go to your friend's house right after hanging up the phone, expecting the pizza to already be in your hands?
The answer is no. The same apply to your problem here. You can't expect Load
to directly return a value (a pizza), while FirebaseDatabase (the pizzailolo) is retrieving the data (the pizza).
Load
needs a function as 2nd parameter that will be called once the data has been retrieved
Your answer
Follow this Question
Related Questions
Delete data in mysql 0 Answers
Get Other Players information (those who are installed) ? 0 Answers
Best DataBase for mobile games 2 Answers
Error Inserting Record - SQLite Database 0 Answers
Best BaaS for storing IAP information 0 Answers