locked
Having trouble getting BackgroundFetch to work in Xamarin Forms iOS. RRS feed

  • Question

  • User234750 posted

    Hi, I've been working on this for days now. I run our XF iOS app on a real device in debug mode. After it starts up, I press Home and the app goes to sleep - a debug statement in OnSleep tells me it got there. Next, I leave it alone and watch for the debug statement that tells me it got to PerformFetch. After 18 hours it still did not appear. I know that iOS controls when it does a PerformFetch. But seriously, 18 hours and nothing? Could iOS have messed up its timer or the minimum time for our app? Is there a way to reset it or does that happen when I reload a newly compiled version? Or do I have to change the actual version number?

    For those that have read this far, you probably have some questions. Here are my settings and some snippets of my code.

    In Info.plist, in Background Modes, I selected Enable Background Modes and Background Fetch.

    On the device: 1) in Settings, I enabled BackgroundAppRefresh for the app. 2) In Settings, under the app, I enabled BackgroundAppRefresh

    ` public override bool FinishedLaunching(UIApplication app, NSDictionary options) { … UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum); }

    public override async void PerformFetch(UIApplication application, Action<UIBackgroundFetchResult> completionHandler)
    {
    try
    {
        var result = UIBackgroundFetchResult.NoData;
            bool bOK = await Util.RequestProjectsFromWebService(user);        
        result = UIBackgroundFetchResult.NewData;
            }
            catch (Exception ex)
            {
        result = UIBackgroundFetchResult.Failed;
            }
            finally
            {
                   completionHandler(result);
            }
    

    } ` I'll be away for a few days, so it will be interesting to see how the app performs during that time.

    Any ideas on how to get into PerformFetch would be much appreciated. Regards, Will

    Friday, July 12, 2019 7:27 PM

Answers

  • User369979 posted

    Could iOS have messed up its timer or the minimum time for our app? Is there a way to reset it or does that happen when I reload a newly compiled version? Or do I have to change the actual version number? Just as you said iOS controls it. We could do nothing about it. What we can do is set the minimum background fetch interval: https://developer.apple.com/documentation/uikit/appandenvironment/preparingyouruitoruninthebackground/updatingyourappwithbackgroundapp_refresh?language=objc But it still doesn' mean it will be triggered every this interval. The documentation says The minimum number of seconds that must elapse before another background fetch can be initiated. This value is advisory only and does not indicate the exact amount of time expected between fetch operations. We have to wait until the device has enough resources to run your application's background fetch.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 16, 2019 10:22 AM

All replies

  • User234750 posted

    Ok - so now it is sort of working. Not sure what changed. Sorry. Behaviour is odd tho. It did a Fetch after 40 minutes and then after 10 min (and I thought there was a 15 minute minimum time between Fetches). Another 40 minute wait and after it finished Fetching I got the nasty message: The app has been terminated. Failed to Stop app: An error occurred on client IDB1610545 while executing a reply for topic xvs/idb/16.1.0.545/stop-app The app has been terminated. I still plan to let it run for a few days and see what happens.

    Friday, July 12, 2019 11:07 PM
  • User369979 posted

    Could iOS have messed up its timer or the minimum time for our app? Is there a way to reset it or does that happen when I reload a newly compiled version? Or do I have to change the actual version number? Just as you said iOS controls it. We could do nothing about it. What we can do is set the minimum background fetch interval: https://developer.apple.com/documentation/uikit/appandenvironment/preparingyouruitoruninthebackground/updatingyourappwithbackgroundapp_refresh?language=objc But it still doesn' mean it will be triggered every this interval. The documentation says The minimum number of seconds that must elapse before another background fetch can be initiated. This value is advisory only and does not indicate the exact amount of time expected between fetch operations. We have to wait until the device has enough resources to run your application's background fetch.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Tuesday, July 16, 2019 10:22 AM
  • User234750 posted

    @LandLu Thanks for your reply. I guess it is what it is and we have to live with it.

    To Anyone Else reading this thread,

    I had set up PerformFetch with SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

    I ran our app over a weekend and it call PerformFetch at these times:

    PerformFetch getting data 2019Jul1214:58:57 PerformFetch getting data 2019Jul1215:46:40 Elapsed time: 48 m PerformFetch getting data 2019Jul1216:48:07 Elapsed time: 1 h 1 m PerformFetch getting data 2019Jul1310:40:10 Elapsed time: 18 h PerformFetch getting data 2019Jul1310:49:10 Elapsed time: 9 m PerformFetch getting data 2019Jul1311:00:43 Elapsed time: 11 m PerformFetch getting data 2019Jul1311:03:11 Elapsed time: 2.5 m PerformFetch getting data 2019Jul1311:19:37 Elapsed time: 16 m PerformFetch getting data 2019Jul1311:28:38 Elapsed time: 9 m PerformFetch getting data 2019Jul1313:22:57 Elapsed time: 1h 54 m PerformFetch getting data 2019Jul1313:32:27 Elapsed time: 9 m PerformFetch getting data 2019Jul1314:09:17 Elapsed time: 36 m PerformFetch getting data 2019Jul13_14:22:27 Elapsed time: 13 m

    And then the app crashed. In summary, PerformFetch got called from a minimum of 2.5 minutes to a maximum of 18 hours with several elaped times of less than 15 minutes (which really surprised me) and several at around an hour.

    Wednesday, July 17, 2019 3:50 PM