1

I have this code to load and count data from API server;

class TestNetWork
  {
    private Task taskFillPicker;
    private List<CityItemDB> itemsCity;
    private CustomPicker cpCity; 

    public async Task FillPicker()
      {
          try {
            JObject res = await SuperFUNC.GET_CITY_ACTIVE_SENDER();
            if(res == null){
              //null
            }else{
              string message = res["message"].ToString();
              if(message.Equals("Success")){

                itemsCity.Clear();
                cpCity.Items.Clear();

                JArray data = (JArray)res["data"];
                int count = data.Count;
                for (int i = 0; i < count; i++) {
                  CityItemDB node = new CityItemDB();
                  node.cityId = Int32.Parse(data[i]["cityId"].ToString());
                  node.cityName = data[i]["cityName"].ToString();

                  itemsCity.Add(node);
                  cpCity.Items.Add(node.ToString());
                }
              }else{
              //null
              }
            }
          } catch (Exception ex) {
            Debug.WriteLine (TAG + " : " + ex.StackTrace);  
          }
      }

    public TestNetWork()
      {
          this.itemsCity = new List<CityItemDB> ();

          this.cpCity = new CustomPicker {
            HeightRequest = 40,
            TextColor = Color.FromHex("#5a5a5a"),
            Title = "City Choose",
          };

          taskFillPicker = FillPicker ();

          Debug.WriteLine (COUNT + " : " + itemsCity.Count);  
      }
  }

But console print me COUNT : 0, I'm sure code get and parse json from internet is correct, picker show full data but List<CityItemDB> itemsCity count 0.

Thank for read, sorry my english not good!

1 Answer 1

1

You need to await the task, otherwise execution might continue before FillPicker has completed:

taskFillPicker = await FillPicker ();

As this code is in a constructor where await is not possible, I suggest moving it to a separate async method:

  public async Task Init()
  {          
      taskFillPicker = await FillPicker ();

      Debug.WriteLine (COUNT + " : " + itemsCity.Count);  
  }

You have to write a little bit more code to construct the object now:

 var n = new TestNetWork();
 await n.Init();
Sign up to request clarification or add additional context in comments.

4 Comments

can't use await in default contructor, sorry i edit the code in question
@HoangQBH yes - my answer suggests a solution for that. Do you have troubles with understanding it?
i know, but i use xamarin, defaule constructor can't be async
Please read my answer carefully :) You have to create a separate method that is async and then call this method after you called the constructor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.