0

在我们当前的 Xamarin 项目中,我遇到了一个奇怪的问题。当应用程序向服务器发送更大的数据块时,为了在应用程序进入后台时保护它,我们正在启动一个长时间运行的任务(使用UIApplication.SharedApplication.BeginBackgroundTask / UIApplication.SharedApplication.EndBackgroundTaskAPI)。奇怪的是,当我从我的机器上构建和运行时,这很有效,但是当我的几个同事在从他们的机器上构建/部署应用程序时运行完全相同的场景时,会出现超时错误。

据我了解,在这样的长期运行任务中运行东西应该可以工作。我不应该在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.
4

1 回答 1

1

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

首先,beginBackgroundTaskWithExpirationHandler:不授予无限的后台活动。最为显着地:

https://developer.apple.com/documentation/uikit/uiapplication/1623031-beginbackgroundtaskwithexpiratio?language=objc

运行后台任务的应用程序有有限的时间来运行它们

其次,NSURLSession在 中默认不启用,并且默认情况下HttpClient,overalNSURLSession不是在后台处理传输的东西,这是可能的,HttpClient不使用这种模式是很自然的。再次检查文档:https ://developer.apple.com/documentation/foundation/nsurlsession

于 2019-07-05T13:23:39.823 回答