背景时,从Xamarin IOS提交数据



我在当前的Xamarin项目中有一个奇怪的问题。当应用程序将较大的数据发送到服务器时,以在应用程序后面保护它时,我们将启动长期运行的任务(使用UIApplication.SharedApplication.BeginBackgroundTask / UIApplication.SharedApplication.EndBackgroundTask API(。很奇怪的是,当我从计算机构建和运行时,这效果很好,但是我的几个同事在从他们的机器中构建/部署应用程序时运行完全相同的方案时会出现超时错误。

据我了解,在这样的长期任务中运行的东西应该有效。我不必在> info.plist 中指定背景功能。另外,由于HttpClient采用NSUrlSession发送/接收,因此应在应用程序背景时保护它免受干扰,对吗?

我无法弄清楚为什么使用不同的MAC构建时相同的代码在同一设备上产生不同的行为。VS中的某个地方是否可以在机器本地进行某个设置?

我现在没有想法,所以任何提示都将不胜感激。

这是根据构建/部署它的MAC的作用/失败代码的示例:

public async Task Submit()
{
    // ... some irrelevant code
    BackgroundTask.Run(async () => await submitAsync()); // Form-level encapsulation of the UIApplication.SharedApplication.BeginBackgroundTask API
    // ... more irrelevant code
}
private async Task submitAsync()
{
    if (!IsSubmitAllowed)
        return;
    IsBusy = true;
    IsGoBackAllowed = IsGoNextAllowed = false;
    var statusIndicator = new StatusIndicator();
    await PopupNavigation.Instance.PushAsync(statusIndicator);
    try
    {
        statusIndicator.Status = "Saving TI";
        statusIndicator.Progress = 0.1;
        var submitted = await _service.SubmitAsync(Model); // ERROR! causes timeout exception for some
        var submittedId = submitted.BackendId;
    // ... etc.

您的两个假设似乎都是错误的。

首先,beginBackgroundTaskWithExpirationHandler:不授予无限的背景活动。最值得注意的是:

https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundTaskwithexpiratio?language=Objc

运行背景任务的应用程序有限的时间 运行它们

第二,NSURLSessionHttpClient中默认不启用,而Overal NSURLSession并不是默认情况下在后台处理转移的东西,这只是可能性,HttpClient不使用此模式很自然。再次检查文档:https://developer.apple.com/documentation/foundation/nsurlsession

最新更新