I'm using the latest Xamarin Forms 3.1.0 and I'm trying to schedule a daily send of information back to the server. I'm using the JobSchedulerService with SetPeriodic(24 * 3600 * 1000) which references the JobService below.
This starts and runs ok but keeps on rescheduling and runs at ever decreasing intervals (as you might expect for a failing task being rescheduled at ever longer intervals).
However, there is nothing in the logcat which indicates an error occurring.
The Logger statements are just an attempt to track what's happening, but I am always seeing the "FINISHED:" statement and never the "FAULTED" message so the task does not seem to be failing What am I doing wrong?
public class AdherenceReportingJob : JobService
{
static readonly string logClassName = typeof(AdherenceReportingJob).ToString();
public override bool OnStartJob(JobParameters jobParams)
{
Logger.Audit(logClassName, "START");
Task.Factory.StartNew(() =>
{
bool updateRan = false;
updateRan = AdherenceCalculator.AdherenceReporting();
// Have to tell the JobScheduler the work is done.
JobFinished(jobParams, false);
Logger.Audit(logClassName, "FINISHED:" + updateRan);
}).ContinueWith(t =>
{
Logger.Error(logClassName, "FAULTED");
Logger.Error(logClassName, t.Exception.InnerException);
JobFinished(jobParams, true);
}, TaskContinuationOptions.OnlyOnFaulted);
// Return true because of the asynchronous work
return true;
}
public override bool OnStopJob(JobParameters jobParams)
{
// we don't want to reschedule the job if it is stopped or cancelled.
Logger.Audit(logClassName, "START");
return false;
}
}
The job is scheduled using:
var javaClass = Java.Lang.Class.FromType(typeof(AdherenceReportingJob));
var componentName = new ComponentName(Application.Context, javaClass);
var jobBuilder = new JobInfo.Builder(ADHERENCE_REPORT_JOB, componentName);
jobBuilder.SetPeriodic(App.OneDay);
jobBuilder.SetRequiredNetworkType(NetworkType.Any); // Need network to report back
var jobInfo = jobBuilder.Build();
var jobScheduler = (JobScheduler) Application.Context.GetSystemService(JobSchedulerService);
var scheduleResult = jobScheduler.Schedule(jobInfo);
if (JobScheduler.ResultSuccess == scheduleResult)
{
Logger.Debug(logClassName, "Scheduled OK");
}
else
{
Logger.Error(logClassName, "Scheduled FAILED");
}
This always reports "Scheduled OK"