-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Android] Essentials - preferences - prevent Application.Quit() from closing application before SharedPreferences in-memory changes are committed to disk. #27635
base: main
Are you sure you want to change the base?
Conversation
Sorry, I know I'm not a maintainer but wanted to throw in my two cents. I'm not sure changing calls from I think finding a way to modify Alternatively, adding something to the MAUI Edit: Thinking about it, the |
@NathanielJS1541 Thanks for your input. |
It's all good! Sorry to be a pain. I will say that I spent a long time looking for alternatives to The only thing I managed to find was this (taken from #27585):
I'm not that experienced with android app lifecycles though, so it could be something obvious. |
@NathanielJS1541, thanks for the details on that.
Don't worry, I'm enjoying investigating this. So you're not a pain at all.😁 |
Current.Set<string?>(key, value, sharedName, commit); | ||
|
||
/// <inheritdoc cref="IPreferences.Set{T}(string, T, string?, bool)"/> | ||
public static void Set(string key, bool value, string? sharedName, bool commit = false) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a note: Adding new public
methods is allowed but such a change will most likely not be accepted for MAUI .NET 9. It can be merged in MAUI .NET 10. Just to warn if you want your PR to be merged in the .NET 9 branch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see. Even if these are just different signatures?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a part of the MAUI team, so they might tell you differently but if you want my honest opinion based on experience of reading many MAUI's PRs, then: Yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I appreciate you're sharing your opinion.
Well, we'll see, I just can't see how we can solve this issue otherwise if I'm honest.
@dotnet-policy-service agree |
Description of Change
Currently, if we use
Application.Quit()
afterPreferences.Set()
, the data is not saved.The reason for this (as mentioned in the issue) is that we are using SharedPreferences.Editor.apply(), which write in memory asynchronously, therefore if we run Application.Quit() just after that. We run the issue of exiting the program before the data gets written in memory.
Using SharedPreferences.Editor.Commit()`, on the other hand, ensures the data is being fully saved before we quit.
Changes
Adding an
commit
argument to thePreferences.Set()
API to allow the user to choose whether to useeditor.commit()
oreditor.Apply()
at the end of the process.By default, it will trigger
editor.Apply()
.This argument is only offered on Android.
Issues Fixed
Fixes #27585