8

What is the correct way to load data async to be able to show UI as fast as possible to user. For example

I have a xamarin forms projekt, containing a map component. I want to show the map before I get the users current location(from gps) and some locations/pins that are fetched from a server. I have seen these two approaches

1) From constructor call an async method

   Map mMyMap;
   ctor()
   {
       InitializeCompeont();
       InitAsync();
   }
   private async void InitAsync()
   { 
      var pins = await GetPinsFromServer();
      mMyMap.Pins.Add(pins)
   }

2) in On appearing

 ctor()
  {
   InitalizeComponent()
  }
  protected override async void OnAppearing()
  { 
      var pins = await GetPinsFromServer();
       mMyMap.Pins.Add(pins)
   }

Both approaces seems to "work", but Im I fooling myself calling the async method from constructor?

Ive also managed to set BindingContext async both ways and it binds correctly

Is there any difference?

1 Answer 1

11

There is a difference in timing and when your load data will be called. If done in the page ctor it will be called once and only once when that page is first created.

If done in OnAppearing the call will happen prior to the page being shown and can be called more than once. For example, if you push another page on top of it and then popped it OnAppearing would get called again reloading your data, which might be okay to do if that other page modified the data being displayed on the previous page. Otherwise, you're potentially making unnecessary load data calls.

It is worth noting that OnAppearing and OnDisappearing aren't always consistently called at the same time on different platforms. For example, if you used the built-in sharing on Android or iOS one might fire both events, but the other might not fire any at all.

Also, I'd make sure you're making use of Task.Run(); to run any long running tasks on a background thread, to ensure you're not locking up the main thread, and potentially set a bool to show/hide a spinner if necessary to know when your background task starts and ends.

3
  • Hi Nick! Thanks for your answer. Ok that clearifies how I should use it. I have a question about the last part though. (Task.Run()...). I Thought my UI-thread was released on await (and I have verified it by looping 50 times (to enlongate time to fetch my map pins and can see that map loads seconds before pins shows up on it)). Are you saying that "await MyLengthyGet()" is not enough to release UI-thread?
    – Cowborg
    Sep 25, 2018 at 8:33
  • I haven't seen your GetPinsFromServer implementation but typically as long as you're using async await all the way throughout your methods and not using a .Result or .Wait anywhere you're usually fine. What I meant by using Task.Run is your method sounds like it could potentially be a long-running task in which case you might consider running it in the background with a CancellationToken because async await itself doesn't necessarily create a new thread and you may want to stop the task depending on what the user does. Sep 25, 2018 at 13:49
  • yes, Im using async await all the way, with out Result.. I think I have to read up on the differnce ... thanks for your help!
    – Cowborg
    Sep 25, 2018 at 14:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.