1

I have an Xamarin IOS Application for which I want to implement a background fetch. When I trigger it on the simulator it works as expected. But on the device it is never executed.

In the AppDelegate "FinishedLaunching" I set the interval to the minimum:

        UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

and I've implemented the perform fetch method:

public override async void PerformFetch(UIApplication application,

Action<UIBackgroundFetchResult> completionHandler)
    {
        UIBackgroundFetchResult fetchResult;
        try
        {
            Debug.Write("Start background fetch.");
            bool canResolve = Mvx.CanResolve<IBusinessFacade>();

            if (!canResolve) return;
            var businessFacade = Mvx.Resolve<IBusinessFacade>();

            //Nach Zeitstempel neu laden
            var reloadChangesResult = await businessFacade.ReloadByTimestamp();

            //Aktualisieren der Kontakte
            ReloadContactsFromAddressBook();
            var resultReload = await businessFacade.ReloadContacts();

            //Ich Kontakt neu laden und bei Änderung Service notifizieren
            var reloadMeResult = await businessFacade.ReloadMeContact();
            if (reloadMeResult.Success)
            {
                if (reloadMeResult.Result.AnyValueChanged || reloadMeResult.Result.HashChanged)
                {
                    var updateMeResult = await businessFacade.UpdateMeContactToService();
                }
            }

            //Notifikationen laden
            var reloadRequestsResult = await businessFacade.ReloadRequests();

            //Pusk aktualisieren
            var pushResult = await businessFacade.RegisterPush();

            // check if there where new data downloaded.
            if (reloadChangesResult.Result || reloadRequestsResult.Result)
            {
                fetchResult = UIBackgroundFetchResult.NewData;
            }
            else
            {
                fetchResult = UIBackgroundFetchResult.NoData;
            }
        }
        catch (Exception ex)
        {
            Debug.Write(ex);
            Analytics.TrackEvent("Exception: Background Fetch", new Dictionary<string, string>
            {
                {"Exception", ex.ToString()}
            });
            fetchResult = UIBackgroundFetchResult.Failed;
        }

        completionHandler(fetchResult);
    }

    private async void ReloadContactsFromAddressBook()
    {
        var addressBook = new AddressBookService();
        var dbFactory = new DbFactory();
        var contactDataService = new ContactDataService(new ContactRepository(dbFactory), new SettingsAdapter(new Settings()));

        var phonebookContacts = addressBook.GetAllContacts();

        HashSet<string> contacts = new HashSet<string>((await contactDataService.GetAllContacts()).Select(r => r.IdAddressbook));

        var listNewContacts = phonebookContacts.Where(c => !contacts.Contains(c.AddressBookId));

        foreach (var contact in listNewContacts)
        {
            ContactEntity ce = new ContactEntity
            {
                IdAddressbook = contact.AddressBookId,
                DisplayName = contact.DisplayName,
                IdService = 0,
                DisplayImage = contact.Image,
                DisplayBirthDate = contact.Events.FirstOrDefault(r => r.EventType == Models.Enums.EventType.Birthday)?.EventDate ?? DateTime.MinValue,
                DisplayPhoneNumber = string.Empty
            };
            await contactDataService.SaveContact(ce);
        }
    }

Background modes are enabled in my info.plist:

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>remote-notification</string>
</array>

I target IOS 10 and above. Xamarin IOS Version: 11.6.1.3

What could be wrong here?

NPadrutt
  • 3,619
  • 5
  • 24
  • 60
  • Does it work on the device when you force it from the debug menu? How long did you wait for your background fetch to occur on the device? I have found that it can take a day or so before iOS schedules a background fetch – Paulw11 Jan 24 '18 at 08:31
  • Unfortunately the Simulate Ios Background Fetch in VS for Mac is greyed out.. also when i start with the (Background Fetch) startup project it just starts my application instead of the background fetch on the device (that works on the simulator though). I started it yesterday aroud 3 in the afternoon and waited till today morning.. so around 17 hours I would say. – NPadrutt Jan 24 '18 at 08:47

0 Answers0