Y
Y
yungarium2018-09-05 10:06:39
iOS
yungarium, 2018-09-05 10:06:39

Why does a Xamarin.iOS application crash when the network is disconnected?

I am using WebClient in Xamarin.iOS. It is necessary to constantly upload the file using the link and transfer the downloaded file to the TextField, everything turned out fine. However, when the network is disconnected, System.Reflection.TargetInvocationException sometimes crashes (although I catch it) and it crashes during operation, not at the beginning. It flies out according to the logic I do not understand "once every other time", let's call it that. The code was debugged, I understood WHERE the problem is, but I don’t understand WHAT it is and HOW to fix it.
1. I turn off the wi-fi network on the phone. The program immediately calls this action

CrossConnectivity.Current.ConnectivityChanged += (sender, args) =>
            {
                try
                {
                    if (CrossConnectivity.Current.IsConnected)
                    {
                        timer.Elapsed += Timer_Elapsed;
                        timer.Start();
                        alertnetworkerror.DismissViewController(true,null);
                    }
                    else
                    {
                        timer.Stop();
                        throw new Exception();
                    }
                }
                catch (Exception)
                {
                    ShowViewController(alertnetworkerror, null);
                    ContainerTextPrice1.Text = "";
                    ContainerTextPrice2.Text = "";
                }
            };

2. The method is called inside: timer.Elapsed += Timer_Elapsed AND THIS IS JUST HERE "every other time" the raw exception is thrown out, the application just crashes
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            DateTime myDate = DateTime.Now;
            InvokeOnMainThread(() =>
            {
                var webClient = new WebClient();
                var url = new Uri("https://blockchain.info/tobtc?currency=USD&value=1");
                webClient.Encoding = Encoding.UTF8;
 
                webClient.DownloadStringCompleted += (s, es) =>
                {
                    var text = es.Result; //  get the downloaded text
                    string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                    string localFilename = "downloaded.txt";
                    string localPath = Path.Combine(documentsPath, localFilename);
                    File.WriteAllText(localPath, text); // writes to local storage
 
                    Decimal.TryParse(text, NumberStyles.Any, CultureInfo.InvariantCulture, out decimal x);
 
                    InvokeOnMainThread(() =>
                    {
                        ContainerTextPrice2.Text = text;
                        ContainerTextPrice1.Text = (1 / x).ToString("#.##");
                    });
                };
 
                webClient.DownloadStringAsync(url);
            });
        }

Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Voronov, 2018-09-05
@newross

The application crashes because there is no error handling in the Timer_Elapsed method. The code that fires on CrossConnectivity.Current.ConnectivityChanged just starts the timer, nothing more.
To eliminate such problems, I recommend switching to RestEase instead of WebClient, it is much more convenient to work with it. And instead of using the Polly timer, there are a lot of examples of how to make a beautiful error handling with a given number of request repetitions in case of failures.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question