So I’m working with Xamarin.iOS and calling an api using async/await. I seem to have encountered a situation that i can’t seem to get past.
I have a button TouchUpInside event that has an async handler and inside the handler i call PerformSegue when a certain event is triggered but that doesn’t work. I can see in the output window that multiple threads are being created and all other lines get executed as expected however the action to PerformSegue is hit and it remains on the same ViewController. I then read on Xamarin tutorial on working with threads where it says
If an async method is called from a background thread (not the main UI thread) then InvokeOnMainThread would still be required.
that also doesn't work.
on the other hand if I remove the async/await from my code and call PerformSegue in the handler then the code works as expected.
Can someone help with a resolution to this situation and a reason why this happens. Thanks
Here' the simplified version of the code:
public override void ViewDidLoad()
{
base.ViewDidLoad();
searchButton.TouchUpInside += SearchButton_TouchUpInside;
}
async private void SearchButton_TouchUpInside(object sender, EventArgs e)
{
var dictionary = new Dictionary<string, string>
{
{"searchText", "abc" }
};
searchItemsResult = await client.InvokeApiAsync<items>("search/searchitems", HttpMethod.Get, dictionary);
if (searchItemsResult != null)
{
//InvokeOnMainThread(() => //doesn't work with this
//{
//tried and doe work with this either
//DispatchQueue.MainQueue.DispatchAsync(() =>
//{
//doesn't push to new ViewController
this.PerformSegue("vC2Segue", this);
//});
//});
}
}