Application.Current.Properties not persisting

Any data I save to Application.Current.Properties seems to clear whenever I restart the Application?

I'm using the following code to save the data:

    private static void SaveLoginLocal ()
    {
        if (Application.Current.Properties.ContainsKey ("LoggedInUser")) {
            Application.Current.Properties ["LoggedInUser"] = User.Current;
        }
        else {
            Application.Current.Properties.Add (new KeyValuePair<string, object> ("LoggedInUser", User.Current));
        }
        Application.Current.SavePropertiesAsync ();
        //LocalDB.DB.Connection.DeleteAll<User> ();
        //LocalDB.DB.Connection.Insert (User.Current);
    }

I used the Application.Current.Properties.Add (new KeyValuePair<string, object> ("LoggedInUser", User.Current)); just to test if creating a new KeyValuePair fixed the problem, but it doesn't.

From the Locals I can see that the data gets saved and I can access it for the lifetime of the application, but as soon as I close it and restart, the data gets cleared. Is this behavior expected or am I doing something wrong?

I'm using:

  • Xamarin Forms V2.0.0.6490
  • Android Platform
  • (Checked that Preserve application data/cache is enabled) - Although I don't even have to deploy my App for the data to clear, just restart it.

Best Answer

Answers

  • BlakeWillsBlakeWills GBMember ✭✭

    Thanks for any help in Advance :)

  • i didn't try out the apis you are using.
    instead, i've done these simple steps and all is going like a charm .
    1) defined a simple interface in shared code

    public interface IPreferenceStore
    {
    void SetString(String p_key, String p_value );
    String GetString(String p_key );
    void SetBool( String p_key, bool p_value);
    bool getBool( String p_key);
    void SetInt( String p_key, int p_value );
    int getInt ( String p_key );
    }

    2) in platform specific code, i have implemented the interface.

    [assembly: Dependency (typeof (forzaJuventus.iOS.PreferenceStore_IOS))]
    namespace RunnerCoach.iOS
    {

    public class PreferenceStore_IOS : IPreferenceStore
    {
        public PreferenceStore_IOS ()
        {
        }
    
        public void SetString( String p_key ,String p_value )
        {
            NSUserDefaults.StandardUserDefaults.SetString(p_value, p_key); 
        }
    
        public void SetInt( String p_key, int p_value )
        {
            NSUserDefaults.StandardUserDefaults.SetInt (p_value, p_key);
        }
    
        public void SetBool( String p_key, bool p_value )
        {
            NSUserDefaults.StandardUserDefaults.SetBool (p_value, p_key);
        }
    
    
        public String GetString(String p_key)
        {
            return NSUserDefaults.StandardUserDefaults.StringForKey (p_key);
        }
        public int getInt(String p_key)
        {
            return (int) NSUserDefaults.StandardUserDefaults.IntForKey (p_key);
        }
        public bool getBool(String p_key)
        {
            return NSUserDefaults.StandardUserDefaults.BoolForKey (p_key);
        }
    }
    

    }

    please note that you need to decorate the assembly to make it visible to xamarin.forms.code

    3) in android code....

    [assembly: Xamarin.Forms.Dependency (typeof (forzaJuve.Droid.PreferenceStore_Droid))]
    namespace RunnerCoach.Droid
    {

    public class PreferenceStore_Droid : IPreferenceStore
    {
        public PreferenceStore_Droid ()
        {
        }
    
        public void SetString( String p_key ,String p_value )
        {
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Forms.Context); 
            ISharedPreferencesEditor editor = prefs.Edit ();
            editor.PutString (p_key, p_value);
            // editor.Commit();    // applies changes synchronously on older APIs
            editor.Apply();   
        }
    
        public void SetInt( String p_key ,int p_value )
        {
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Forms.Context); 
            ISharedPreferencesEditor editor = prefs.Edit ();
            editor.PutInt (p_key, p_value);
            // editor.Commit();    // applies changes synchronously on older APIs
            editor.Apply();   
        }
    
        public void SetBool( String p_key ,bool p_value )
        {
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Forms.Context); 
            ISharedPreferencesEditor editor = prefs.Edit ();
            editor.PutBoolean (p_key, p_value);
            // editor.Commit();    // applies changes synchronously on older APIs
            editor.Apply();   
        }
    
    
    
        public String GetString( string p_key )
        {
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Forms.Context); 
            return prefs.GetString (p_key, "");
        }
    
        public int getInt( string p_key )
        {
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Forms.Context); 
            return prefs.GetInt (p_key, 0);
        }
    
        public bool getBool( string p_key )
        {
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences (Forms.Context); 
            return prefs.GetBoolean (p_key, false);
        }
    
    }
    

    }

    4) later....in shared code.... i made a static class, consuming dependencyService.
    Hope this help you. :)

    namespace forzaJuve.Common
    {

    static class PreferenceStore
    {
        public static String GetString(String p_key )
        {
            return Xamarin.Forms.DependencyService.Get<IPreferenceStore> ().GetString(p_key); 
        }
    
        public static bool GetBool(String p_key )
        {
            return Xamarin.Forms.DependencyService.Get<IPreferenceStore> ().getBool(p_key); 
        }
    
        public static int GetInt(String p_key )
        {
            return Xamarin.Forms.DependencyService.Get<IPreferenceStore> ().getInt(p_key); 
        }
    
    
        public static void SetString(String p_key, String p_value )
        {
            Xamarin.Forms.DependencyService.Get<IPreferenceStore> ().SetString(p_key,p_value); 
        }
        public static void SetBool(String p_key, bool p_value )
        {
            Xamarin.Forms.DependencyService.Get<IPreferenceStore> ().SetBool(p_key,p_value); 
        }
        public static void SetInt(String p_key, int p_value )
        {
            Xamarin.Forms.DependencyService.Get<IPreferenceStore> ().SetInt(p_key,p_value); 
        }
    }
    

    }

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    Use @JamesMontemagno settings plugin

  • @AlessandroCaliaro how could we grow and become independent if at the first problem we use third parties plugins?
    we need more knowledge...not more plugins.
    Or if you prefer, we need a more robust platform. i believe in Xamarin Forms ... but it has to become adult.

  • BlakeWillsBlakeWills GBMember ✭✭

    @alessandrosuppiej - I agree, and I appreciate your answer, however your suggested implementation is essentially a 3rd party solution. (You wrote the code so your the third party) :smile:

    The docs says this should work and its a one-line piece of code, I'd rather Xamarin fixed the bug (if it is one) or told me what I was doing wrong, so I can use the recommended method of storing data in the Application Properties instead of relying on some 3rd party code that isn't part of the Xamarin Platform. :neutral:

  • pfedotovskypfedotovsky RUMember ✭✭

    Any news regarding the issue?

  • AlessandroCaliaroAlessandroCaliaro ITMember ✭✭✭✭✭

    This bug should be solved in current alpha

Sign In or Register to comment.