Api call wait on mobile

Eduardo Gomez Romero 965 Reputation points
2024-11-22T09:36:43.77+00:00

For some reason, the api that I am calling in the home page, in windows is loaded when I start the app, but on mobile, I have to wait

Question

Can I do something to make it faster? Why in desktop I do not have to wait?

Demo

https://reccloud.com/u/niick4m

HomePage

  public partial class HomePageViewModel : ObservableObject
  {
      private readonly DeviceLanguageService deviceLanguageService;

      private readonly HttpService httpService;

      public Command<object> OnPinMarkerClickedCommand { get; set; }

      protected readonly TurbinesService _turbinesService;

      public ObservableCollection<TurbinePin> Turbines => _turbinesService.TurbinePins;

      public HomePageViewModel(HttpService service, DeviceLanguageService deviceLanguage, TurbinesService turbinesService)
      {
          deviceLanguageService = deviceLanguage;

          httpService = service;

          LoadNews();

          _turbinesService = turbinesService;

          OnPinMarkerClickedCommand = new Command<object>(OnPinMarkerClicked);

          _turbinesService.GetTurbinePinsForUI(OnPinMarkerClickedCommand);

      }

      public ObservableCollection<Article>? NewsList { get; set; } =
          [];

      async void LoadNews()
      {

          var language = deviceLanguageService.GetDeviceCultureInfo();

          var newsUrl = AppConstants.GetNewsUrl(language.TwoLetterISOLanguageName);

          NewsList!.Clear();

          var newsObj = await httpService.GetAsync<News>(newsUrl);

          foreach (var item in newsObj!.Articles!)
          {
              NewsList.Add(item);
          }
      }

Service

 public class HttpService(HttpClient httpClient, IConnectivity connectivity, ILogger<HttpService> logger)
 {
     public async Task<T?> GetAsync<T>(string url)
     {
         if (connectivity.NetworkAccess != NetworkAccess.Internet)
         {
             await Shell.Current.DisplayAlert("Error",
                 "No internet connectivity"
                 , "OK");
             return default;
         }
         // Add User-Agent header
         httpClient.DefaultRequestHeaders.Add
             ("User-Agent",
             "Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
         try
         {
             var response =
                     await httpClient.GetAsync(url);
             if (response.IsSuccessStatusCode)
             {
                 var jsonData = await response.Content.ReadAsStringAsync();
                 var data = await response.Content.ReadFromJsonAsync<T>();
                 return data;
             }
         }
         catch (Exception e)
         {
             logger.LogError($"Http Service: {e.Message}");
         }
         return default;
     }


What I am trying to do is to make the desktop and mobile the same

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,666 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.