'Xamarin - Shell.Current.GoToAsync($"//...") performance too slow on Android
I have a Xamarin project and I'm using visual studio 2019. I consume an API to access data and for my LoginPage flow I used routing in shell.
So here is my AppShell which calls firstly LoginPage.
<ShellItem Route="login">
<ShellContent ContentTemplate="{DataTemplate local:LoginPage}"/>
</ShellItem>
<TabBar Route="main">
<Tab Title="Browse" Icon="tab_feed.png">
<ShellContent ContentTemplate="{DataTemplate local:CategoryPage}" />
</Tab>
</TabBar>
And then on my LoginPage I check if the user is already logged in on my OnAppearing method and then on Login_Clicked I check the user enters the right credentials
protected override async void OnAppearing()
{
base.OnAppearing();
if (CurrentPropertiesService.IsAuth())
{
await Shell.Current.GoToAsync($"//main");
}
}
private async void Login_Clicked(object sender, EventArgs e)
{
ApiService apiService = new ApiService();
var userDto = new AuthUser
{
User = username.Text,
Password = password.Text
};
var token = await apiService.LoginAsync(Constants.Url, userDto);
if(token != "")
{
await Shell.Current.GoToAsync($"//main");
}
else
{
await DisplayAlert("Error", "Credenciales inválidos", "Ok");
}
}
So I think this shell routing that I'm using is making the performance really slow. But in case it were the api calls the reason of this, here is my ApiService
public async Task<string> LoginAsync (string url, AuthUser data)
{
var user = await HttpPostAsync(url, data);
if(user != null)
{
//Save data on constants
CurrentPropertiesService.SaveUser(user);
return user.Token;
}
else
{
return string.Empty;
}
}
public async Task<BaseEntity<CrCategory>> GetCategories(string url, string token)
{
return await HttpGetAsync<BaseEntity<CrCategory>>(url, token);
}
public async Task<BaseEntity<CrProduct>> GetProductsByCategoryAndCompany(int category, int company, string token)
{
string url = string.Concat(Constants.UrlProduct, "?IdCompany=", company, "&IdCategory=", category);
return await HttpGetAsync<BaseEntity<CrProduct>>(url, token);
}
public async Task<BaseEntity<CrUserInfo>> GetUserInfo(string username, string token)
{
string url = string.Concat(Constants.UrlUserInfo, "?Username=", username);
return await HttpGetAsync<BaseEntity<CrUserInfo>>(url, token);
}
public async Task<BaseUserEntity<CrUser>> GetUserById(int id, string token)
{
string url = string.Concat(Constants.UrlUser, "/", id);
return await HttpGetAsync<BaseUserEntity<CrUser>>(url, token);
}
// Generic Get Method
private async Task<T> HttpGetAsync<T>(string url, string token)
{
T result = default(T);
try
{
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
HttpResponseMessage response = httpClient.GetAsync(url).Result;
HttpContent content = response.Content;
if (response.IsSuccessStatusCode)
{
var jsonResponse = await content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
}
else
{
throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
}
}
catch (Exception ex)
{
OnError(ex.ToString());
}
return result;
}
// Generic Post Method
private async Task<T> HttpPostAsync<T>(string url, T data)
{
T result = default(T); // résultat de type générique
try
{
string json = JsonConvert.SerializeObject(data);
StringContent content = new StringContent(json, Encoding.UTF8, "application/json");
var response = await httpClient.PostAsync(new Uri(url), content);
if (response.IsSuccessStatusCode)
{
var jsonResponse = await response.Content.ReadAsStringAsync();
result = JsonConvert.DeserializeObject<T>(jsonResponse);
}
else
{
throw new Exception(((int)response.StatusCode).ToString() + " - " + response.ReasonPhrase);
}
return result;
}
catch (Exception ex)
{
OnError(ex.ToString());
return result;
}
}
If you now why this is happening please help.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
