0

I have a multi-platform app which pulls current location and uses that in a loop to calculate the distance to each item. It is using

wineriesView.ItemsSource = await App.WineryManager.GetWineries(seltyp,selval,selwrs);

to load each item which contains latitude and longitude, among other things. It is calling

public Task<List<WineryItem>> GetWineries(string typ, string val, int wrs) { return restService.ReadWineries(typ,val,wrs); }

which then calls

    public async Task<List> ReadWineries(string typ, string val, int wrs)
{
        var position = await Geolocation.GetLocationAsync();
        var uri = new Uri(string.Format(Constants.WineriesUrl, "wineries_by_region.php?region=piedmont"));
        var urio = new Uri(string.Format(Constants.OnlinesUrl, "onlines_by_id.php?id=12"));
        Items = new List<WineryItem>();
        OItems = new List<OnlineItem>();
    ...

var content = await response.Content.ReadAsStringAsync();
            Records = JsonConvert.DeserializeObject<WineryRecords>(content);
            Items = new List<WineryItem>();
            foreach (var record in Records.records)
            {
                Location winePosition = new Location(Convert.ToDouble(record.Latitude), Convert.ToDouble(record.Longitude));
                double dblDistance = Location.CalculateDistance(position, winePosition, DistanceUnits.Miles);

...

if (dblDistance > 0 && dblDistance < 1000)
                    {
                        item.intDistance = Convert.ToInt16(dblDistance);
                        item.strDistance = dblDistance.ToString("N0")+"Mi";
                    } else
                    {
                        item.intDistance = 0;
                        item.strDistance = "--";
                    }
                    Items.Add(item);

With ReadWineries executing all of the code shown, it only returns 10 items to the list, whereas it will return the entire list of 50+ if I comment the geolocation, location, and calculate distance code. How do I extend the timeout for this task so that it will return all items even though it takes longer? I am debugging this on an Android device. Will it make a difference if I use an iOS device? In the end, it will need to run on both. I have tried setting the accuracy on getlocation but that doesn't help. It seems like the app is getting bogged down with each call to calculate distance. Any ideas?

1 Answers1

0

How do I extend the timeout for this task so that it will return all items even though it takes longer?

If you want to get this goal, you can use Task.Delay and Task.WhenAny to do this.

Here is the same article that you can take a look:

C# Xamarin Forms - Executing task, with timeout

Cherry Bu - MSFT
  • 10,160
  • 1
  • 10
  • 16