Slow startup time and collection

Eduardo Gomez Romero 905 Reputation points
2024-11-20T16:15:51.9433333+00:00

I do not know what Microsoft did in .net 9, I am experiencing a weird error, where even if I put a startup time of 2 seconds, and it takes 13 seconds

this is my startup page

  public partial class StartupPageViewModel : ObservableObject {

      [RelayCommand]
      async Task Appearing() {

          // Wait for 2 seconds
          await Task.Delay(2500);

          // Then navigate to HomePage
          await Shell.Current.GoToAsync($"//{nameof(HomePage)}", true);
      }
  }


<ContentPage
    x:Class="METROWIND.Views.StartupPage"
    xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
    xmlns:mct="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
    xmlns:rex="clr-namespace:METROWIND.Resources"
    xmlns:vm="clr-namespace:METROWIND.ViewModel"
    x:DataType="vm:StartupPageViewModel">
    <ContentPage.Behaviors>
        <mct:EventToCommandBehavior
            Command="{x:Binding AppearingCommand}"
            EventName="Appearing" />
    </ContentPage.Behaviors>
    <Grid
        HorizontalOptions="Center"
        RowDefinitions="Auto,*,*"
        RowSpacing="20"
        VerticalOptions="Center">
        <Image
            Aspect="Center"
            Source="metro_wind_200.png" />
        <Image
            Grid.Row="1"
            Style="{x:StaticResource StartupScreen}" />
        <Label
            Grid.Row="2"
            Style="{x:StaticResource startupScreenSubtitle}" />
    </Grid>
</ContentPage>


And in my collection page, my collection view, doesn't show right away

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;

    [ObservableProperty]
    bool isLoading;

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

        IsLoading = true;

        deviceLanguageService = deviceLanguage;

        _turbinesService = turbinesService;

        httpService = service;

        OnPinMarkerClickedCommand = new Command<object>(OnPinMarkerClicked);

        _turbinesService.GetTurbinePinsForUI(OnPinMarkerClickedCommand);

        LoadNews();

    }

Service

   public class TurbinesService {
       private const string collectionName = "turbines";
       private readonly DeviceLanguageService _deviceLanguageService;
       private readonly FirestoreService _firestoreService;
       private readonly BlobServiceClient _blobServiceClient;
       private static System.Timers.Timer? _timer;
       private FirestoreDb? _firestoreDb;

       public ObservableCollection<TurbinePin> TurbinePins { get; set; } = [];

       public TurbinesService(DeviceLanguageService deviceLanguageService,
           FirestoreService firestoreService, BlobServiceClient blobServiceClient) {
           _deviceLanguageService = deviceLanguageService;
           _firestoreService = firestoreService;
           _blobServiceClient = blobServiceClient;
           InitializeAsync();
       }

       private async void InitializeAsync() {
           await _firestoreService.InitializeFirestoreAsync();
           _firestoreDb = _firestoreService.GetFirestoreDb();
           if (_firestoreDb != null) {
               await LoadOrInitializeTurbineAsync();
               //InitializeTimer();
           }
       }

       async Task LoadOrInitializeTurbineAsync() {
           var turbinesRef = _firestoreDb!.Collection(collectionName);
           var snapshot = await turbinesRef.GetSnapshotAsync();

           if (snapshot.Count == 0) {
               var turbine = new Turbine {
                   Id = "EC-G-SB",
                   Country = "Ecuador",
                   Name = "Estación Ciudadela Simón Bolívar",
                   Address = "Av. de las Américas, Guayaquil 090513, Ecuador",
                   Latitude = -2.151993,
                   Longitude = -79.886109,
                   InstalationDateTime = new DateTime(2024, 8, 2, 0, 0, 0, DateTimeKind.Utc),
                   ImagesURL = [],
               };

               turbine.RemovedCo2Kilograms = Math.Round(turbine.EnergyProduced * turbine.Co2EmissionOffset, 5);

               await AddTurbineImages(turbine);

               var turbineDocRef = turbinesRef.Document(turbine.Id);

               await turbineDocRef.SetAsync(turbine);

               AddToObservable(turbine);
           }
           else {
               foreach (var document in snapshot.Documents) {
                   var turbine = document.ConvertTo<Turbine>();
                   turbine.Id = document.Id;
                   await AddTurbineImages(turbine);
                   AddToObservable(turbine);
               }
           }
       }

       private async Task AddTurbineImages(Turbine turbine) {

           turbine.ImagesURL!.Clear();

           var containerClient = _blobServiceClient.GetBlobContainerClient(turbine.Country!.ToLower());
           await foreach (var item in containerClient.GetBlobsAsync()) {
               var blobClient = containerClient.GetBlobClient(item.Name);
               turbine.ImagesURL!.Add(blobClient.Uri.ToString());
           }
       }

       private void AddToObservable(Turbine turbine) {
           TurbinePins.Add(new TurbinePin { Turbine = turbine });
       }

       public ObservableCollection<TurbinePin> GetTurbinePinsForUI(ICommand pinClickedCommand) {
           foreach (var pin in TurbinePins.OrderBy(t => t.Turbine?.InstalationDateTime)) {
               pin.PinClickedCommand = pinClickedCommand;
           }
           return TurbinePins;
       }

Demo

https://reccloud.com/u/9t6d9u2

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,650 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.