Best background processing/API call approach?

I've come up with two different approaches for handling an API call off the main UI thread, and I'm wondering which one is better in terms of performance or resource allocation. These are Android-specific examples, but I have a similar setup for iOS as well, so looking for an answer with cross-platform consideration.

Option 1 - Kick off a new thread for the request, and handle response in the callback on the UI thread:

        apiTestButton.Click += delegate
        {
            progressBar1.Visibility = ViewStates.Visible;

            new Thread(new ThreadStart(() =>
            {
                string response = Core.BLL.API.GetJsonTest();

                RunOnUiThread(() => ApiTestCallback(response));
            })).Start();
        };

        void ApiTestCallback(string response)
        {
            progressBar1.Visibility = ViewStates.Invisible;

            Toast.MakeText(this.ApplicationContext, "API response: " + response, ToastLength.Long)
                 .Show();
        }

Option 2 - Leverage async/await:

        apiAsyncTestButton.Click += async (sender, e) =>
        {
            progressBar1.Visibility = ViewStates.Visible;

            string response = Core.BLL.API.GetJsonTestAsync().Result;

            progressBar1.Visibility = ViewStates.Invisible;

            Toast.MakeText(this.ApplicationContext, "API response: " + response, ToastLength.Long)
                 .Show();
        };

Using async/await, the code is obviously a bit more concise - no need for a separate callback method. But I'm wondering how these two options stack up in terms of performance. I assume Xamarin will translate/transform C# into platform-native logic (iOS and Android), but which would be easier for it to handle and are there performance implications?

Thanks in advance.

Sign In or Register to comment.