Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by adrenalmedulla · Jul 29, 2018 at 09:59 PM · databasedatabase handlingsnapshotlambda

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?,

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0

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!" ) ) ;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hellium · Jul 31, 2018 at 05:19 PM 0
Share

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");
     }
  }

avatar image adrenalmedulla Hellium · Jul 31, 2018 at 05:29 PM 0
Share

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

avatar image Hellium adrenalmedulla · Jul 31, 2018 at 06:02 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

154 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges