1

I can simulate the background fetch using iOS simulator. Is it possible to simulate the expiration so it can call the expire handler? I tried to use a infinite loop and run as background fetch on an simulator but doesn't seem to trigger it.

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);
            });
LittleFunny
  • 8,155
  • 15
  • 87
  • 198
  • Start debugging your app, once it is running, place it into the background (Home button, either on psychical device or Hardware/Home on Sim), then Invoke background fetch via Run / Background Fetch in IDE. Your expiration handler should be invoked about 30 seconds later (assuming you have not return from the PerformFetch method (hang it via a async Task.Delay or something.... ) – SushiHangover Sep 26 '17 at 02:53
  • I looping it for 2hrs but the app killed and not calling the handler when I have placed a bulletpoint. – LittleFunny Sep 26 '17 at 03:09
  • Sorry it works using your Task.Delay method. I wonder how long will it allow before the app kill when calling the handler... When I put the breakpoint, the debugger will reach to the spot and later the app killed. Looks like it's not last long either right. – LittleFunny Sep 26 '17 at 03:46
  • When the expiration handler is called your app is being killed, so yes, you only have a couple seconds. I post an analytics "Report", it gets logged and queued to send, but normally the app is killed before it get sent thus I do not see it until the app is run again. – SushiHangover Sep 26 '17 at 04:06

1 Answers1

0

Here is a sample out of one of my apps:

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...");
}
  • Start debugging your app

  • Place it into the background

    • via Home button, either on physical device or Hardware/Home on Sim
  • Invoke background fetch via Run / Simulate iOS Background Fetch in IDE.

The expiration handler should be invoked about 30 seconds later. You have a few seconds to handle any cleanup/logging.

SushiHangover
  • 73,120
  • 10
  • 106
  • 165