Is the Clearing of Microsoft.Maui.Storage.Preferences a bug when updating to Maui 6.0.486?

dg2k 1,386 Reputation points
2022-08-03T21:23:39.927+00:00

I noticed all persisted app settings implemented by Microsoft.Maui.Storage.Preferences were cleared when I updated Visual Studio to Version 17.3.0 Preview 6.0 (Maui version 6.0.486). The Preferences Clear() method was never directly used, but after the said update, persisted setting values were observed to be in cleared state. By that I mean even supplied default values were not picked up.

For instance, for the following, DatabaseName became null rather than the default name supplied.

public static string DatabaseName  
{  
    get => Preferences.Get(nameof(DatabaseName), $"{AppInfo.Name}.db");  
    set => Preferences.Set(nameof(DatabaseName), value);  
}  

While a constant string could be used (and avoid the trouble), there is a need to change the name, for instance, when the database is encrypted. In fact, the failure on null value for database name on start-up revealed the whole problem.

The same was true for the following where SelectedLanguage showed up as null and easy to speculate how things can go wrong.

public static string SelectedLanguage  
{  
    get => Preferences.Get(nameof(SelectedLanguage), "en");  
    set => Preferences.Set(nameof(SelectedLanguage), value);  
}  

The same was the case for integer values clearing to 0 even when the supplied default values are non-zero.

Setting types used are mostly Strings, Booleans and Integers with very few DateTime and total entries around 30. No similar issues were observed with Xamarin over a long time and with a number of published apps.

Could this be a recent Maui bug to be rectified soon, or could someone clarify if there are things to watch out for Microsoft.Maui.Storage.Preferences such as what impacts unintentional clearing of Preference keys and values?

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,977 questions
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 69,386 Reputation points Microsoft Vendor
    2022-08-04T06:06:11.277+00:00

    Hello,​

    Could this be a recent Maui bug to be rectified soon, or could someone clarify if there are things to watch out for Microsoft.Maui.Storage.Preferences such as what impacts unintentional clearing of Preference keys and values?

    Yes, I tested it in Maui version 6.0.419, I can get the value correctly in Android, windows and iOS.

    But when I upgraded Maui version to 6.0.486, I got the same issue. Please report this issue on MAUI GitHub page.

    I found a workaround to fix this issue, you can judge the value Preferences.Get(nameof(DatabaseName), $"{AppInfo.Name}.db") , if the value is Null or whitespace, we can set a value like following code.

       public static string DatabaseName  
           {  
       		get {   
       			  
       			if(string.IsNullOrWhiteSpace( Preferences.Get(nameof(DatabaseName), $"{AppInfo.Name}.db")))  
       			{  
                       Preferences.Set(nameof(DatabaseName), $"{AppInfo.Name}.db");  
                   }  
                   return Preferences.Get(nameof(DatabaseName), $"{AppInfo.Name}.db");  
         
                   }  
               set => Preferences.Set(nameof(DatabaseName), value);  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful