Having trouble getting BackgroundFetch to work in Xamarin Forms iOS.

Will.ItraxWill.Itrax CAMember ✭✭✭

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

Best Answer

Answers

  • Will.ItraxWill.Itrax CAMember ✭✭✭

    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.

  • Will.ItraxWill.Itrax CAMember ✭✭✭

    @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 2019Jul12_14:58:57
    PerformFetch getting data 2019Jul12_15:46:40 Elapsed time: 48 m
    PerformFetch getting data 2019Jul12_16:48:07 Elapsed time: 1 h 1 m
    PerformFetch getting data 2019Jul13_10:40:10 Elapsed time: 18 h
    PerformFetch getting data 2019Jul13_10:49:10 Elapsed time: 9 m
    PerformFetch getting data 2019Jul13_11:00:43 Elapsed time: 11 m
    PerformFetch getting data 2019Jul13_11:03:11 Elapsed time: 2.5 m
    PerformFetch getting data 2019Jul13_11:19:37 Elapsed time: 16 m
    PerformFetch getting data 2019Jul13_11:28:38 Elapsed time: 9 m
    PerformFetch getting data 2019Jul13_13:22:57 Elapsed time: 1h 54 m
    PerformFetch getting data 2019Jul13_13:32:27 Elapsed time: 9 m
    PerformFetch getting data 2019Jul13_14: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.

Sign In or Register to comment.