I have 3 ObservableCollections in my ViewModel and one Class that I load when the application starts. To ensure deserialization of ObservableCollections I just got it.
if(SomeCollection.Count == 0) ThisCollection = await deserializationMethod<ObservableColletion<T>>(filename);
If the file does not exist, deserializationMethod will create a new object with
return Activator.CreateInstance<T>();
This works great - no problem with that.
And for class I
if(ClassObject.Loaded != true) ThisObject = await deserializationMethod<T>(filename);
I added a property - if the file is deserialized, then it is correct. This seems to work, but it is NOT. This happens very rarely, but sometimes the file is not deserialized, and when you use the application, this file is overwritten, so all data is destroyed. I can not find the cause of the problem. This is what you just launch the application, and it happens - just 100 starts.
How to be sure that if a file exists, then it will be deserialized for sure? Or maybe I should make a List these ObservableCollections + Class and serialize it into a single file? Is there any good practice with this?
EDIT:
I used SemaphoreSlim to make sure everything is used as intended, but today it happened again.
The fact is that the application starts, and nothing else is even listened to. Nothing is being said at the moment. It appears that the data is not deserialized or does not read an existing file. Since all changes are recorded when the application is closed, everything disappears. Any other ideas what they can be and how to be sure of data deserialization?
EDIT FINAL - playback problem:
I finally reproduced what was happening. Therefore, I removed the fixes with code that is not needed here.
I have a BackPressed event to handle when the user returns or wants to exit the application (if on MainPage ). This piece of code seems to be causing the problem. What happens for sure. Firstly, the problem cannot be reproduced using the emulator. My BackPressed method contained a wait using a serialization method that saved data that was later removed (since Ondrei Sveidar wrote what he wrote before reading). BUT I started checking it out, and there is strange behavior, and I still have some questions about this.
How does this happen.
When I started the application (for example, by accident) and the screen loaded, I press the back button several times β the application does not work, it closes as soon as possible, and I donβt even see the user interface (sometimes I can see the AppBar for a moment). Then, when I try to open the application again (no matter, immediately or later), it βresumesβ, and after that moment my data disappeared. But not all data. Only the latter is saved with the wait in the BackPressed method. Just this. I tried to save one, two and three ObservableCollections with and without this class, and ALWAYS the last one was kept "empty". After that, I got the Application.Current.Exit () method, which can call this, but I'm not sure if it matters when the serialization method is Task, and only the last one is serialized incorrectly.
When I remove this from the BackPressed method, I cannot reproduce this problem, so this is it.
I still have questions: Is this behavior expected? Is there a better way to close the application and serialize the data, or do I just have to save it while using the application without leaving it?