1

我可以使用 iOS 模拟器模拟后台获取。是否可以模拟过期,以便它可以调用过期处理程序?我尝试使用无限循环并在模拟器上作为后台获取运行,但似乎没有触发它。

task = UIApplication.SharedApplication.BeginBackgroundTask("bgEntityDownload", () =>
            {
                AppLogger.Instance.AddLog(AppLogLevel.Information,
                                      nameof(BgProcess),
                                      nameof(DownloadEntityFromServer),
                                      "Background Fetch Expired", "");
                App.CurrentDataStatus.HasSync = new EntityQueueBLL().HasData(siteId);
                UIApplication.SharedApplication.EndBackgroundTask(task);
                task = UIApplication.BackgroundTaskInvalid;
                System.Diagnostics.Debug.WriteLine("Ending .....");
                completionHandler?.Invoke(UIBackgroundFetchResult.NewData);
            });
4

1 回答 1

0

这是我的一个应用程序的示例:

public async override void PerformFetch(UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
{
    var result = UIBackgroundFetchResult.NoData;
    try
    {
        await Task.Run(async () =>
        {
            var iOSTaskId = UIApplication.SharedApplication.BeginBackgroundTask("MyBackgroundTask", ExpirationHandler);
            //result = await BackgroundFetchBoxOfficeTicketResults();
            await Task.Delay(60 * 1000);
            result = UIBackgroundFetchResult.NewData;
            UIApplication.SharedApplication.EndBackgroundTask(iOSTaskId);
        });
    }
    catch // Fetch Errors are Reported via analytics system within BackgroundFetchBoxOfficeTicketResults
    {
        result = UIBackgroundFetchResult.Failed;
    }
    completionHandler(result);
}

void ExpirationHandler()
{
    //Report(ProgID, PerFormFetchFailedID, DeviceID, $"User:{UserID}, Network:{NetworkType}");
    Console.WriteLine("beginBackgroundTaskWithName:expirationHandler: called...");
}
  • 开始调试您的应用

  • 将其置于背景中

    • 通过 Home 按钮,无论是在物理设备上还是在 Sim 上的 Hardware/Home
  • 在 IDE 中通过Run/调用后台获取。Simulate iOS Background Fetch

过期处理程序应在大约 30 秒后调用。您有几秒钟的时间来处理任何清理/日志记录。

于 2017-09-26T03:59:23.473 回答