后台提取不在设备上执行

时间:2018-01-24 08:02:55

标签: ios xamarin.ios background-fetch

我有一个Xamarin IOS应用程序,我想要实现后台提取。当我在模拟器上触发它时,它按预期工作。但是在设备上它永远不会被执行。

在AppDelegate“FinishedLaunching”中,我将间隔设置为最小值:

perform id, status FROM users WHERE id in( 2,4,7,6) order by id FOR UPDATE;

我已经实现了执行获取方法:

        UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum);

我的info.plist中启用了后台模式:

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);
        }
    }

我的目标是IOS 10及以上版本。 Xamarin IOS版本:11.6.1.3

这里可能有什么问题?

0 个答案:

没有答案