diff --git a/Covid19Radar/Covid19Radar.iOS/AppDelegate.cs b/Covid19Radar/Covid19Radar.iOS/AppDelegate.cs index c36334acb..4545e8092 100644 --- a/Covid19Radar/Covid19Radar.iOS/AppDelegate.cs +++ b/Covid19Radar/Covid19Radar.iOS/AppDelegate.cs @@ -26,6 +26,7 @@ using System.Threading.Tasks; using Prism.Ioc; using Covid19Radar.Repository; +using BackgroundTasks; namespace Covid19Radar.iOS { @@ -95,10 +96,11 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary launchOpt global::Xamarin.Forms.Forms.Init(); global::Xamarin.Forms.FormsMaterial.Init(); + _loggerService.Value.Info("Initialized xamarin.forms"); + FFImageLoading.Forms.Platform.CachedImageRenderer.Init(); global::FFImageLoading.ImageService.Instance.Initialize(new FFImageLoading.Config.Configuration()); - _notificationCenterDelegate.OnRecieved += async (UserNotificationCenterDelegate sender, UNNotificationResponse response) => { var navigationParameters = new NavigationParameters(); @@ -115,6 +117,8 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary launchOpt UIApplication.SharedApplication.SetMinimumBackgroundFetchInterval(UIApplication.BackgroundFetchIntervalMinimum); + // Registration of all launch handlers must be complete before the end of applicationDidFinishLaunching(_:). + // https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler/3180427-register ScheduleBackgroundTasks(); return base.FinishedLaunching(app, launchOptions); @@ -122,6 +126,8 @@ public override bool FinishedLaunching(UIApplication app, NSDictionary launchOpt private void ScheduleBackgroundTasks() { + _loggerService.Value.StartMethod(); + try { _exposureDetectionBackgroundService.Value.Schedule(); @@ -148,6 +154,21 @@ private void ScheduleBackgroundTasks() { _loggerService.Value.Exception("Failed to schedule DataMaintainanceBackgroundService", exception); } + + _loggerService.Value.EndMethod(); + } + + private void LoggingPendingTaskRequests() + { + _loggerService.Value.Info($"Get pending task requests"); + BGTaskScheduler.Shared.GetPending(pendingTaskRequests => + { + _loggerService.Value.Info($"Pending task count: {pendingTaskRequests.Length}"); + foreach (var pendingTaskRequest in pendingTaskRequests) + { + _loggerService.Value.Info($"Identifier: {pendingTaskRequest.Identifier}, EarliestBeginDate: {pendingTaskRequest.EarliestBeginDate}"); + } + }); } private bool IsUniversalLinks(NSDictionary launchOptions) @@ -262,6 +283,15 @@ public override void OnActivated(UIApplication uiApplication) { base.OnActivated(uiApplication); MessagingCenter.Send((object)this, AppConstants.IosOnActivatedMessage); + + try + { + LoggingPendingTaskRequests(); + } + catch (Exception ex) + { + _loggerService.Value.Exception("Failure get pending task requests", ex); + } } private void RegisterPlatformTypes(IContainer container) diff --git a/Covid19Radar/Covid19Radar/App.xaml.cs b/Covid19Radar/Covid19Radar/App.xaml.cs index 448208526..ac0ce81ca 100644 --- a/Covid19Radar/Covid19Radar/App.xaml.cs +++ b/Covid19Radar/Covid19Radar/App.xaml.cs @@ -39,6 +39,7 @@ public partial class App : PrismApplication private IBackupAttributeService BackupAttributeService; private IEventLogRepository EventLogRepository { get; set; } + private IEventLogService EventLogService { get; set; } /* * The Xamarin Forms XAML Previewer in Visual Studio uses System.Activator.CreateInstance. @@ -61,6 +62,7 @@ protected override void OnInitialized() BackupAttributeService.SetSkipBackupAttributeToEventLogDir(); EventLogRepository = Container.Resolve(); + EventLogService = Container.Resolve(); LogUnobservedTaskExceptions(); @@ -243,19 +245,14 @@ private static void RegisterCommonTypes(IContainer container) protected override async void OnStart() { - LogFileService.Rotate(); - - await EventLogRepository.RotateAsync( - AppConstants.EventLogFileExpiredSeconds); + base.OnStart(); + await ExecuteBackgroundTask(); } protected override async void OnResume() { base.OnResume(); - LogFileService.Rotate(); - - await EventLogRepository.RotateAsync( - AppConstants.EventLogFileExpiredSeconds); + await ExecuteBackgroundTask(); } protected override void OnSleep() @@ -270,5 +267,30 @@ private void LogUnobservedTaskExceptions() // maybe think local only logger }; } + + private async Task ExecuteBackgroundTask() + { + LoggerService.StartMethod(); + + try + { + LogFileService.Rotate(); + + await EventLogRepository.RotateAsync( + AppConstants.EventLogFileExpiredSeconds); + + await EventLogService.SendAllAsync( + AppConstants.EventLogMaxRequestSizeInBytes, + AppConstants.EventLogMaxRetry); + } + catch (Exception ex) + { + LoggerService.Exception("Failed to manually execute background task", ex); + } + finally + { + LoggerService.EndMethod(); + } + } } }