0

If I make a web service request and background the iOS app and foreground it again, I get an exception that the request has been canceled. Is this a bug or the way iOS operates? I can't find any documentation on this. What's the right way to implement a reliable web service mechanism using Xamarin Forms? I followed the HttpClient example as documented here.

user246392
  • 2,661
  • 11
  • 54
  • 96
  • That is the way iOS works by default. You can look at the various iOS backgrounding techniques to see what would best fit your use-case https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/index / https://learn.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks#background_tasks_in_iOS_7 – SushiHangover Jun 15 '19 at 22:22
  • That is a pretty good example @user246392 and is pretty reliable, you just need to make sure your app handles exceptions gracefully. Here's a good deep dive into iOS https://stackoverflow.com/questions/6650717/when-an-ios-application-goes-to-the-background-are-lengthy-tasks-paused – Saamer Jun 16 '19 at 00:20

1 Answers1

0

This works for me both in the foreground and in the background. maybe you also need to enable background modes in your info.plist

private async Task<string> UsaHttpClient()
 do
 {
  ...
  ...
  ...
  await Task.Run(async () =>
     {
     HttpClient Client = new HttpClient();
     Client.BaseAddress = new Uri("https://services.xxxxx.com/ServiceApi/");
     var response = Client.GetStringAsync("api/function?Parametro...").Result;
     HttpResponseMessage resp = new HttpResponseMessage();
     resp.Content = new StringContent(response, Encoding.UTF8, "application/json");
     r = response;
     resp.Dispose();
     Client.Dispose();
     await Task.Delay(second for delay);
     });
 } while (X == anyCondition);
}