I use Lazy initialization for app singleton in Xamarin.Forms (app runs on iOS):
public sealed class DataSingleton
{
private static readonly Lazy<DataSingleton> lazy = new Lazy<DataSingleton>(() => new DataSingleton(), LazyThreadSafetyMode.PublicationOnly); // tried withou 2nd parameter as well
public static DataSingleton Instance
{
get { return lazy.Value; }
}
...
}
And i call it in webserver which runs to provide data for Front-end which is in Angular (used web view to show Angular code)
var server = new WebServer(o => o
.WithUrlPrefix($"http://localhost:{appSettings.ApiPort}")
.WithMode(HttpListenerMode.EmbedIO))
.WithCors()
.WithLocalSessionManager()
.WithWebApi("/api", m => m
.WithController<TestController>()
.WithController<SettingsController>()
.WithController<ReportController>()
.WithController<SystemController>())
.WithModule(new ActionModule("/", HttpVerbs.Any,
ctx => ctx.SendDataAsync(new { Message = "Error" })))
.RunAsync();
In controllers is called DataSingleton to get/set data, but after app returns from background, DataSingleton.Instance is null.
What should I do to don't lose data of singleton, while app is in background for short time (approximately 5 minutes)
Update - I've figured out that this problem is only in Controllers, cause when app gets back to front I can see all the data in AppDelegate
WillEnterForegroundevent..