Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hieumoscow/7bde368ecd479f8a85bce15fb838ade9 to your computer and use it in GitHub Desktop.
Save hieumoscow/7bde368ecd479f8a85bce15fb838ade9 to your computer and use it in GitHub Desktop.
nint taskId = -1;
Task savingChoiceTask;
public async override void DidEnterBackground (UIApplication app)
{
Logger.Log ("DidEnterBackground() - entered background");
// Begin Finite-Length Task.
this.taskId = app.BeginBackgroundTask (null);
// Start saving the user choices.
// Note that the method will by default execute on the UI thread. If you go back to the app while the method
// is running, you won't see any UI updates and the app will eventually crash. That's why we run it on a separate thread.
if (savingChoiceTask == null || savingChoiceTask.IsCompleted) {
savingChoiceTask = Task.Run (() => this.SaveUserChoices ());
await savingChoiceTask;
}
// End Finite-Length Task, finished.
if (this.taskId != -1)
{
UIApplication.SharedApplication.EndBackgroundTask (this.taskId);
this.taskId = -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment