简体   繁体   中英

NSUserDefaults not saving StandardUserDefaults values

I have an iOS application developed using Microsoft's Xamarin, which is why some of this code might look different. In any case, here is what I'm trying to do:

In my application, I have a set of default user settings that need to be in every single instance of the application no matter what. This is set using this method:

    public static void SetDefaults()
    {
        var keys = new object[]
        {
            "LastNotificationTime",
            "LastSyncTime",
            "LunchStartTime",
            "LunchEndTime",
            "OnLunchBreak",
            "PunchedIn",
            "PunchTime",
            "ShowDetailPane",
            "NotifyEmergency",
            "RunAtBoot",
            "RowsPerPage",
            "SyncInterval"
        };
        var values = new object[]
        {
            "",
            "",
            "",
            "",
            false,
            false,
            "",
            true,
            true,
            false,
            5,
            60
        };
        var defaults = NSDictionary.FromObjectsAndKeys(values, keys);
        NSUserDefaults.StandardUserDefaults.RegisterDefaults(defaults);
        NSUserDefaults.StandardUserDefaults.Synchronize();
    }

This works great during the initial run of my application. I will deploy on my device from scratch, run the application, and then stop. I will then redeploy the already-installed application. I don't want to wipe out the defaults and set truly-default values every time, so I perform this check before doing so (in this example, Settings.GetPreferences() returns the NSUserDefaults.StandardUserDefaults object):

    if( Settings.GetPreferences().IntForKey("RowsPerPage") == 0 || 
        Settings.GetPreferences().IntForKey("RowsPerPage") == null )
    {
        Settings.SetDefaults();
    }

As you can see above, RowsPerPage is initially set to 5. I am expecting this value, for each run after the subsequent initial installation, to persist as being 5. Or whatever the user chooses, for that matter.

However, the above check always comes out as true, and my SetDefaults() method is called, wiping out any changes the user made to their Defaults during the last run. Does anyone know why this is happening? Am I doing something wrong when setting these defaults? Even after adding the not-recommended, deprecated NSUserDefaults.StandardUserDefaults.Synchronize() method call, the defaults are still reset.

Your issue is that RegisterDefaults doesn't do what you think it does:

From the documentation

The contents of the registration domain are not written to disk; you need to call this method each time your application starts.

If you want to establish initial values for your defaults that persist then you will need an individual set operation for each one in your SetDefaults method, rather than using RegisterDefaults

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM