Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 77050

Async api call On startUp

$
0
0

Hi,

We have the folllowing issue:
When our app starts we want to do an async api call to get the logged in user.
When de api call completes we store the user object in a static values class.
But the app already starts building up pages etc based on the user in de static values class before the api call is completed so we get errors on that pages because the user is not set yet.
In the constructor we have the following code:

 public partial class MainPage : TabbedPage
    {
        public MainPage()
        {
            InitializeComponent();
            Task taskA = Task.Factory.StartNew(() => GetUserFromApi());
            taskA.Wait();
            this.Children.Add(new NavigationPage(GetActionsPage()) { Title = "Acties" });
            this.Children.Add(new NavigationPage(GetScanPage()) { Title = "Scannen" });
            this.Children.Add(new NavigationPage(GetMainSearchPage()) { Title = "Winkelen" });
            this.Children.Add(GetShoppingCartPage());
            this.Children.Add(new NavigationPage(GetProfilePage()) { Title = "Profiel" });
        }

        private async void GetUserFromApi()
        {
            WebshopUserViewModel model = new WebshopUserViewModel(AppValues.WebshopUserFilter);
            WebshopUser user = await model.LogWebShopUserIn();
            AppValues.LoggedInWebshopUser = user;

        }
}

But the pages are already building before the api calls is completed.
How can we make sure that the api call is finished before the pages are build up?


Viewing all articles
Browse latest Browse all 77050

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>