jamesmontemagno / SettingsPlugin

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

Some settings are not persisted in iOS 13 #158

Closed naweed closed 4 years ago

naweed commented 5 years ago

Description On iOS 13, some settings are not persisted. Other are working fine. This only started happening since last week after I pushed an update to my app (with new Xamarin Forms version and XCode 11.2).

Expected Behavior All settings should be persisted.

Actual Behavior Some users of the app are complaining that some of the settings are not persisted (e.g. bool value in my case), while others are persisted.

Basic Information Version with issue: 3.1.1 Last known good version: 3.1.1 IDE: Visual Studio 2019 Platform Target Frameworks: iOS: Target 11.0, Built with Xcode 11.2 Nuget Packages: Xam.Plugins.Settings (3.1.1), Xamarin.Forms (4.3.0.947036) Affected Devices: iOS 13 (some devices)

Sample Code

private static ISettings AppSettings { get { return CrossSettings.Current; } }

//This doesn't work private bool _applicationPurchased = false; public bool ApplicationPurchased { get { _applicationPurchased = AppSettings.GetValueOrDefault(nameof(ApplicationPurchased), false); return _applicationPurchased; } set { SetProperty(ref _applicationPurchased, value); AppSettings.AddOrUpdateValue(nameof(ApplicationPurchased), _applicationPurchased); } }

//This works fine private string _applicationLanguage; public string ApplicationLanguage { get { _applicationLanguage = AppSettings.GetValueOrDefault(nameof(ApplicationLanguage), "EN"); return _applicationLanguage; } set { SetProperty(ref _applicationLanguage, value); AppSettings.AddOrUpdateValue(nameof(ApplicationLanguage), _applicationLanguage); } }

naweed commented 5 years ago

Btw, I noticed that it is only happening to new users of the app. Older users are fine.

Regards Naweed

Bejasc commented 4 years ago

I'm also experiencing this issue. Seems to only be a problem after upgrading XCode and Visual Studio. All builds compiled since the upgrade have had problems reading values from the settings.

I have tried this with CrossSettings.Current, and with the new Xamarin.Essentials.Preferences - I'm experiencing the same behaviour from both.

Android builds are working as expected / no change in behavior. This functionality had been working for months up until now.

It seems to occasionally be reading the default value - even though the value has been saved already.

(Most frequently reproduced like so) Fresh install of App. First load - set the preferences. Shut app. Second load - Read the preferences. Only reading defaults. Set the preferences again. Shut App Third load - Reads preferences correctly Every other consecutive load - Reads preferences correctly

Again - experiencing this with both this plugin, and SettingsPlugin.

@jamesmontemagno any pointers?

naweed commented 4 years ago

I was able to identify the issue. Something changed in Xamarin Forms version which caused the two way bindings to behave differently. For me, after some troubleshooting, I was able to fix it by switching the order in setter. If I update my local variable first and then persist the setting, then it works fine.

private string _applicationLanguage; public string ApplicationLanguage { get { _applicationLanguage = AppSettings.GetValueOrDefault(nameof(ApplicationLanguage), "EN"); return _applicationLanguage; } set { SetProperty(ref _applicationLanguage, value); AppSettings.AddOrUpdateValue(nameof(ApplicationLanguage), _applicationLanguage); } }

Plz try and see if this works fine.

Regards Naweed

jamesmontemagno commented 4 years ago

You should always use: AppSettings.AddOrUpdateValue(nameof(ApplicationLanguage), value);

setting value will ensure that it is set correct :)