I’m making a pedometer/exercise tracker the place I’m attempting to each rely steps and in addition learn accelorometer knowledge constantly when the applying enters the background. I’m utilizing the BGProcessingTaskRequest from the BackgroundTasks library. Nevertheless, when the cellphone display screen is locked, the app will not course of knowledge (I’m sending a small MQTT message every time when debugging).
I’m creating on a Home windows machine onto a bodily iPhone 12. I’ve enabled all the pieces background associated in data.plist, together with background, fetch and so on.
All code is from the App Delegate in app-name/mission/Platforms/iOS
/AppDelegate.cs
I’m registering like this.
public override bool FinishedLaunching(UIApplication software, NSDictionary launchOptions)
{
Console.WriteLine("FinshedLaunching referred to as ...");
BGTaskScheduler.Shared.Register("com.area.com.backgroundprocessing", null, (activity) =>
{
Console.WriteLine("Registered about to name backgroundprocessing");
PerformBackgroundprocessing((success) =>
{
Console.WriteLine("Backgroundprocessing !!!");
activity.SetTaskCompleted(success);
});
});
I’m scheduling like this:
public override void DidEnterBackground(UIApplication software)
{
base.DidEnterBackground(software);
Console.WriteLine("Entered background");
nint taskId = 0;
// Start a background activity to increase execution time
taskId = software.BeginBackgroundTask(() =>
{
// Deal with expiration of background activity
software.EndBackgroundTask(taskId);
});
var appTaskProcessingRequest = new BGProcessingTaskRequest("dk.au.besafe.backgroundprocessing")
{
EarliestBeginDate = NSDate.Now
};
BGTaskScheduler.Shared.Submit(appTaskProcessingRequest, out NSError error2);
if (error2 != null)
{
Console.WriteLine($"Error submitting backgroundprocessing request: {error2.LocalizedDescription}");
}
else
{
Console.WriteLine("Background processing activity request submitted efficiently. Ought to run inside 10-20 minutes");
}
// Finish the background activity
software.EndBackgroundTask(taskId);
}
I’m processing like this:
/// <param identify="completionHandler"></param>
personal async void PerformBackgroundProcessing(Motion<bool> completionHandler)
{
// Carry out background processing right here
// This methodology can be referred to as when the system determines it is acceptable to carry out background processing
Console.WriteLine("PerformBackgroundprocessing referred to as");
//await ConnectAsync();
// As soon as the background processing is full, name the completion handler with the suitable consequence
// Go true if processing was profitable, in any other case cross false
completionHandler(true);
}