0

I am developing a MAUI application that fetches data from an API (CoinDesk API) to display current Bitcoin price data. The data fetching works fine in Debug mode, but the API call fails to show any data in Release mode. Here is my code for fetching the data:

using Acr.UserDialogs;
using Seraphis.Model;
using System;
using System.Diagnostics;
using System.Net.Http;
using System.Text.Json;
using System.Threading.Tasks;

namespace Seraphis.Api
{
    class FetchCoinDeskDataApi
    {
        private readonly HttpClient _client;

        public FetchCoinDeskDataApi()
        {
            _client = new HttpClient();
        }

        public async Task<string> FetchCoinDeskDataAsync()
        {
            string url = "https://api.coindesk.com/v1/bpi/currentprice.json";

            try
            {
                var response = await _client.GetStringAsync(url);

                if (string.IsNullOrEmpty(response))
                {
                    Debug.WriteLine("Empty response received.");
                    UserDialogs.Instance.Toast("No data received from API.");
                    return null;
                }

                Debug.WriteLine($"Response: {response}");

                return response;
            }
            catch (HttpRequestException httpEx)
            {
                Debug.WriteLine($"HTTP Error: {httpEx.Message}");
                UserDialogs.Instance.Toast("Failed to fetch data. Please check your internet connection.");
                return null;
            }
            catch (Exception ex)
            {
                Debug.WriteLine($"Error: {ex.Message}");
                UserDialogs.Instance.Toast("An error occurred while fetching the data.");
                return null;
            }
        }
    }
}

Steps I have taken so far:

API works fine in Debug mode, but in Release mode it doesn't show the data. Error handling: I added try-catch blocks to log errors in case of failure, but nothing shows up in Release mode. HttpClient initialization: I am reusing the HttpClient across the lifetime of the app. Permissions: I have verified that the necessary internet permissions are declared for both Android (AndroidManifest.xml) and iOS (Info.plist). Logging: I use Debug.WriteLine for logging, but it doesn't show up in Release mode.

Things I suspect:

Linker/Trimming Issue: It’s possible that some parts of the code are being trimmed or removed during Release compilation. Network-related Permissions: There might be issues with network access in Release mode. Timeout/Network Issues: There could be some differences between Debug and Release that cause network requests to fail in Release mode.

What I have tried:

I have confirmed that the API endpoint is accessible, and there are no issues with the API itself. Tried to disable trimming in the .csproj file for debugging purposes (false). I also ensured that internet permissions are set correctly.

What I am looking for:

Why is the data showing up in Debug mode but not in Release mode? How can I fix this issue so that the API call works in Release mode as well? Any advice on how to debug API issues in Release mode or configurations that may need adjustment for Release?

Any help would be greatly appreciated!

4
  • 1
    how is the ui defined to show this data? Commented Nov 12, 2024 at 5:32
  • 1
    I feel lucky today, so I am going to do a stab in the dark: stackoverflow.com/a/75285858/6643940 Commented Nov 12, 2024 at 7:55
  • @Sweathkumar as Bhavanesh requested already, please show how you bind to the data. If you're using compiled bindings (i.e. x:DataType="SomeType"), as H.A.H. suggests, then you will need to use them all-the-way. Commented Nov 13, 2024 at 14:58
  • Could you please show the code about displaying the data? In addition, did you try to check the problem is api data not received or data got not displayed on UI? Commented Nov 18, 2024 at 5:27

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.