-1

So, I am trying to get my data from User table on Firebase and after I return and try to set the result to a Label the screen just go full white (I am using xamarin for android if it matter) and nothing happen. Maybe I do something wrong or I missed something, I am also kinda blurred on how task work. I anyone have any idea please fell free to give me any directions or any other alternatives. This is the code I used .Thank you

UserDetailsRetrive.cs

public async Task<UserDetails> getUserInfoDetails()
        {
            FirebaseClient client = new FirebaseClient("https://xyz.firebaseio.com/");
            return (UserDetails)(await client.Child("user").OnceAsync<UserDetails>()).Select(item => new UserDetails
            {
                first_name = item.Object.first_name,
                last_name = item.Object.last_name,
                username = item.Object.username,
                birthdate = item.Object.birthdate,
                newsletter = item.Object.newsletter,
                term_condition = item.Object.term_condition
            });

Profile.cs

 public void setUserInfo()
        {

            UserDetailsRetreive usr = new UserDetailsRetreive();
            UserDetails userDetails = usr.getUserInfoDetails().Result;
            username.Text = userDetails.username.ToString();


        }

UserDetails.cs

class UserDetails
    {
        public string first_name { get; set; }
        public string last_name { get; set; }
        public string username { get; set; }
        public string birthdate { get; set; }
        public bool newsletter { get; set; }
        public bool term_condition { get; set; }


    }
FakeSeven
  • 1
  • 2

1 Answers1

0

Task.Result would block until Task is completed, which why you experience freezing of UI. For solving this, you need to call usr.getUserInfoDetails asynchronously

public async Task setUserInfo()
{
    UserDetailsRetreive usr = new UserDetailsRetreive();
    UserDetails userDetails = await usr.getUserInfoDetails();
    username.Text = userDetails.username.ToString();
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51
  • Thank you. It worked but also had to call it like this ` new Action(async () => await setUserInfo())();` – FakeSeven Dec 01 '19 at 08:53
  • @Gelu-ValericăCojocariu if the calling method signature could be changed, then you could change that as well so that setUserInfo could be called asynchronously without using Action. – Anu Viswan Dec 01 '19 at 15:28
  • Glad to help you.Kindly mark it as Answer/Useful if you found it useful – Anu Viswan Dec 01 '19 at 15:29