我可以使用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);
});
答案 0 :(得分: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...");
}
开始调试您的应用
将其放入背景
在IDE中通过Run
/ Simulate iOS Background Fetch
调用后台提取。
应该在大约30秒后调用过期处理程序。您有几秒来处理任何清理/日志记录。