jamesmontemagno / SettingsPlugin

Read and Write Settings Plugin for Xamarin and Windows
MIT License
324 stars 80 forks source link

java.long => java.string parsing error by getting a saved DateTime #136

Closed 8-Ball85 closed 6 years ago

8-Ball85 commented 6 years ago

Failure to fill out this information will result in this issue being closed.

If you are creating an issue for a BUG please fill out this information. If you are asking a question or requesting a feature you can delete the sections below.

If you post a full stack trace in a bug it will be closed, please post it to http://gist.github.com and then post the link here.

Bug Information

Reading DateTime value to string throws an Java.Lang.ClassCastException Version Number of Plugin: 3.1.1 Device Tested On: Android Phone, PC Simulator Tested On: - Version of VS: 2017 Version of Xamarin: 2.5.1.527436 Versions of other things you are using:

Steps to reproduce the Behavior

Expected Behavior

Saved DateTime should be a string in DateTime Format like 19.06.2018 14:44:35

Actual Behavior

The output is something like "-6123548784253" in UWP and Android crashes with described exception

Code snippet

public static DateTime InstallationDate
        {
            get
            {
//here is the point where the app crashes
                string date = ApplicationSettings.GetValueOrDefault(nameof(InstallationDate), null).ToString();
                if(date != null)
                {
                    DateTime dateTime;
                    if (DateTime.TryParse(date, out dateTime))
                    {
                        return dateTime;
                    }
                }
                return DateTime.UtcNow;
            }
            set
            {
                ApplicationSettings.AddOrUpdateValue("InstallationDate", value);
                ApplicationSettings.AddOrUpdateValue("InstallationDateSet", true);
            }
        }

Screenshots

jamesmontemagno commented 6 years ago

You are requesting back a string, but saving a DateTime. You MUST get/set the same value type else it can not be parsed. The library automatically does this for you. Do NOT set NULL for the default, give it a datetime value as the default, or save the value as a string.