0

I have this code that is called when a button is pressed:

async void OnTapped(object sender, System.EventArgs e)
{
    var btn = sender as ButtonTemplate;
    if (btn == null)
        return;
    if (btn.Text == Settings.cc.ShortText())
        return;
    if (Counts.phaseTableSelectedCardCount != 0)
    {
        var canContinue = await this.DisplayAlert("Selector", "Changing this will remove all previously selected cards from the deck", "OK", "Cancel");
        if (canContinue == false)
            return;
    }
    var settingId = SetButtons(btn.Text);
    //
    // These updates take 1-3 seconds
    //
    App.DB.UpdateSelected(false);
    App.DB.UpdateIntSetting(SET.Cc, settingId);
    AddDetailSection();
}

public void AddDetailSection()
{
    vm.IsBusy = true;
    Change.cardSelection = true;
    App.DB.GetData();
    detailsLayout.Children.Clear();   
    detailsLayout.Children.Add(CreateDetailSection(null));
    SetPageDetails();
    vm.IsBusy = false;
}

After the button is pressed the SetButtons(btn.Text); code changes the button colors. However I do not see the effect on the UI until 2-3 seconds later when these lines have executed,     

App.DB.UpdateSelected(false);
App.DB.UpdateIntSetting(SET.Cc, settingId);
AddDetailSection();

Is there a way that the UI colors can change and the above three lines can run in the background?

Samantha J T Star
  • 30,952
  • 84
  • 245
  • 427
  • 3
    Consider using the `async` `await` pattern – TheGeneral Aug 03 '18 at 08:52
  • I would like to use that pattern but I am not sure how to use that in this instance as the onTapped is already async.  Could you give an example of what you mean using my code? – Samantha J T Star Aug 03 '18 at 08:59
  • 1
    Short answer you dont, you use it in your `App.DB.GetData();`, `await` and use the *Async* version of any database call, return a `Task` with the `async` keyword, and so-on and so-forth. this will free up the UI thread so you can see the UI changes – TheGeneral Aug 03 '18 at 09:01
  • Note that it’s just the AddDetailSection(); call that takes a long time.  the other two updates are very quick and I am not concerned about them – Samantha J T Star Aug 03 '18 at 09:02
  • 1
    Possible duplicate of [How to properly make asynchronous / parallel database calls](https://stackoverflow.com/questions/35374697/how-to-properly-make-asynchronous-parallel-database-calls) – Biesi Aug 03 '18 at 09:03
  • Can someone give an example of how I could do this in an answer as I am not sure how to do this and how to specify things like the return parameter from my async method. – Samantha J T Star Aug 03 '18 at 09:18

1 Answers1

0

Try something like here

  async void OnTapped(object sender, System.EventArgs e)
  {
     var btn = sender as ButtonTemplate;
     if (btn == null)
        return;
     if (btn.Text == Settings.cc.ShortText())
        return;
     if (Counts.phaseTableSelectedCardCount != 0)
     {
        var canContinue = await this.DisplayAlert("Selector", "Changing this will remove all previously selected cards from the deck", "OK", "Cancel");
        if (canContinue == false)
           return;
     }
     var settingId = SetButtons(btn.Text);
     await Task.Run(() => AddDetailSection(settingId));
  }

  public void AddDetailSection(int settingId)
  {
     App.DB.UpdateSelected(false);
     App.DB.UpdateIntSetting(SET.Cc, settingId);
     AddDetailSection();
     vm.IsBusy = true;
     Change.cardSelection = true;
     App.DB.GetData();
     detailsLayout.Children.Clear();
     detailsLayout.Children.Add(CreateDetailSection(null));
     SetPageDetails();
     vm.IsBusy = false;
  }
BeeTee
  • 1