I'm trying this code from Xamarin. Ios documentation for background tasks: https://docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/backgrounding/ios-backgrounding-techniques/ios-backgrounding-with-tasks. It says that the variable taskId
in line 6 has CS0841: Cannot use local variable 'taskId' before it is declared
. How should I assign the taskId
? Also, how do you declare myFlag
? I cannot find any descriptions of how to use this variable.
Task.Factory.StartNew( () => {
//expirationHandler only called if background time allowed exceeded
var taskId = UIApplication.SharedApplication.BeginBackgroundTask(() => {
Console.WriteLine("Exhausted time");
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});
while(myFlag == true)
{
Console.WriteLine(UIApplication.SharedApplication.BackgroundTimeRemaining);
myFlag = SomeCalculationNeedsMoreTime();
}
//Only called if loop terminated due to myFlag and not the expiration of time
UIApplication.SharedApplication.EndBackgroundTask(taskId);
});
CodePudding user response:
This is the correct link : iOS Backgrounding with Tasks .
How should I assign the taskId?
As it's mentioned in the link , taskId
is a unique identifier which returns from the registration process ,we just need to pass it when calling EndBackgroundTask
.
nint taskID = UIApplication.SharedApplication.BeginBackgroundTask( () => {});
UIApplication.SharedApplication.EndBackgroundTask(taskId);
how do you declare myFlag?
The variable just means the flag if ending the background task , just set it as true
if you don't want to stop the task .