Reputation: 365
I am calling a methodgetaddress();
inside viewmodel for getting address information from API. But while debugging, execution returns at
var response = await client.PostAsync(base_url + "api_delivery_details", formcontent).ConfigureAwait(false);
to the calling method getaddress()
but executes the remaining portion later.so getting null result. How to execute all from first time?
public async void Get_branch_products()
{
RestClient restClient = new RestClient();
getaddresses(); //this is the calling method
long delivery_fee1 = long.Parse(delivery_fee);
GrandTotal = Total + (Total * Tax) / 100 + delivery_fee1 ;
OnPropertyChanged("GrandTotal");
tax_amt = (Total * Tax) / 100;
OnPropertyChanged("tax_amt");
}
public async void getaddresses()
{
ind_vis = true;
OnPropertyChanged("ind_vis");
int user_id = (int)App.Current.Properties["user_id"];
RestClient restClient = new RestClient();
var result = await restClient.Get_address(user_id); //calling restAPI method
if (result.status == "success")
{
ind_vis = false;
OnPropertyChanged("ind_vis");
public async Task<Address> Get_address(int user_id)
{
var formcontent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string,string>("user_id",user_id.ToString())
});
//returns from here// var response = await client.PostAsync(base_url + "api_delivery_details", formcontent).ConfigureAwait(false);
//executes later// var result = await response.Content.ReadAsStringAsync();
var Items = JsonConvert.DeserializeObject<Address>(result);
return Items;
}
Upvotes: 0
Views: 240
Reputation: 16449
Always remember one thing while creating background threads using async-await
, async voids
unless a built-in event handler are never to be used.
Since async voids are fire and forget types and cause a lot of conundrum.
What you should use is a Task
or a Task<T>
instead.
I would suggest you read this awesome guide by MSDN on Async and Await Best Practices where they have explained everything so well if you read it.
Now jumping to your code you need to have the following changes in your code so that this issue does not happen
execution returns at
var response = await client.PostAsync(base_url + "api_delivery_details", formcontent).ConfigureAwait(false);
to the calling method getaddress() but executes the remaining portion later.so getting null result. How to execute all from first time?
Change your code like below:
public async Task Get_branch_products()
{
RestClient restClient = new RestClient();
await GetAddresses(); //this is the calling method
long delivery_fee1 = long.Parse(delivery_fee);
GrandTotal = Total + (Total * Tax) / 100 + delivery_fee1 ;
OnPropertyChanged("GrandTotal");
tax_amt = (Total * Tax) / 100;
OnPropertyChanged("tax_amt");
}
public async Task GetAddresses()
{
ind_vis = true;
OnPropertyChanged("ind_vis");
int user_id = (int)App.Current.Properties["user_id"];
RestClient restClient = new RestClient();
var result = await restClient.Get_address(user_id); //calling restAPI method
if (result.status == "success")
{
ind_vis = false;
OnPropertyChanged("ind_vis");
public async Task<Address> Get_address(int user_id)
{
var formcontent = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string,string>("user_id",user_id.ToString())
});
//returns from here// var response = await client.PostAsync(base_url + "api_delivery_details", formcontent).ConfigureAwait(false);
//executes later// var result = await response.Content.ReadAsStringAsync();
var Items = JsonConvert.DeserializeObject<Address>(result);
return Items;
}
Once this is done your code will wait for the GetAddresses
method to get you the data and it will then move further.
Upvotes: 2