Skip to content
This repository has been archived by the owner on Apr 12, 2023. It is now read-only.

Commit

Permalink
Merge pull request #1092 from cocoa-mhlw/feature/background-task-bug
Browse files Browse the repository at this point in the history
バックグラウンドタスクの処理の見直し
  • Loading branch information
cocoa-dev004 authored Jul 27, 2022
2 parents 3407e4d + 1cd165f commit 5268c94
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 9 deletions.
32 changes: 31 additions & 1 deletion Covid19Radar/Covid19Radar.iOS/AppDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Threading.Tasks;
using Prism.Ioc;
using Covid19Radar.Repository;
using BackgroundTasks;

namespace Covid19Radar.iOS
{
Expand Down Expand Up @@ -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();
Expand All @@ -115,13 +117,17 @@ 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);
}

private void ScheduleBackgroundTasks()
{
_loggerService.Value.StartMethod();

try
{
_exposureDetectionBackgroundService.Value.Schedule();
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
38 changes: 30 additions & 8 deletions Covid19Radar/Covid19Radar/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -61,6 +62,7 @@ protected override void OnInitialized()
BackupAttributeService.SetSkipBackupAttributeToEventLogDir();

EventLogRepository = Container.Resolve<IEventLogRepository>();
EventLogService = Container.Resolve<IEventLogService>();

LogUnobservedTaskExceptions();

Expand Down Expand Up @@ -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()
Expand All @@ -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();
}
}
}
}

0 comments on commit 5268c94

Please sign in to comment.