Add IDisposable to Preferences

This will allow us to call Synchronize on iOS on dispose instead of every single interaction with NSUserDefualts
This commit is contained in:
Jonathan Dick 2018-02-21 10:12:48 -05:00
Родитель d5e276e2e2
Коммит fc0eb709db
5 изменённых файлов: 68 добавлений и 21 удалений

Просмотреть файл

@ -126,5 +126,17 @@ namespace Xamarin.F50
PreferenceManager.GetDefaultSharedPreferences(Application.Context) :
Application.Context.GetSharedPreferences(SharedName, FileCreationMode.Private);
}
bool disposedValue = false;
void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing) { }
disposedValue = true;
}
}
}
}

Просмотреть файл

@ -138,5 +138,21 @@ namespace Xamarin.F50
string.IsNullOrWhiteSpace(SharedName) ?
NSUserDefaults.StandardUserDefaults :
new NSUserDefaults(SharedName, NSUserDefaultsType.SuiteName);
bool disposedValue = false;
void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing && userDefaults != null)
{
userDefaults.Synchronize();
userDefaults.Dispose();
userDefaults = null;
}
disposedValue = true;
}
}
}
}

Просмотреть файл

@ -20,5 +20,10 @@ namespace Xamarin.F50
T Get<T>(string key, T defaultValue) =>
throw new NotImplentedInReferenceAssembly();
}
void Dispose(bool disposing)
{
throw new NotImplentedInReferenceAssembly();
}
}
}

Просмотреть файл

@ -3,13 +3,13 @@ using System.Collections.Generic;
using System.Text;
namespace Xamarin.F50
{
/// <summary>
/// Shared code between preferences
/// Contains static methods and shared members
/// </summary>
public partial class Preferences
{
{
/// <summary>
/// Shared code between preferences
/// Contains static methods and shared members
/// </summary>
public sealed partial class Preferences : IDisposable
{
public Preferences()
{
}
@ -21,18 +21,20 @@ namespace Xamarin.F50
public string SharedName { get; private set; }
public string Get(string key, string defaultValue)
=> Get<string>(key, defaultValue);
public bool Get(string key, bool defaultValue)
=> Get<bool>(key, defaultValue);
public int Get(string key, int defaultValue)
=> Get<int>(key, defaultValue);
public double Get(string key, double defaultValue)
=> Get<double>(key, defaultValue);
public float Get(string key, float defaultValue)
=> Get<float>(key, defaultValue);
public long Get(string key, long defaultValue)
=> Get<long>(key, defaultValue);
public string Get(string key, string defaultValue) =>
Get<string>(key, defaultValue);
public bool Get(string key, bool defaultValue) =>
Get<bool>(key, defaultValue);
public int Get(string key, int defaultValue) =>
Get<int>(key, defaultValue);
public double Get(string key, double defaultValue) =>
Get<double>(key, defaultValue);
public float Get(string key, float defaultValue) =>
Get<float>(key, defaultValue);
public long Get(string key, long defaultValue) =>
Get<long>(key, defaultValue);
public void Dispose() =>
Dispose(true);
}
}

Просмотреть файл

@ -20,5 +20,17 @@ namespace Xamarin.F50
T Get<T>(string key, T defaultValue) =>
throw new NotImplentedInReferenceAssembly();
bool disposedValue = false;
void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing) { }
disposedValue = true;
}
}
}
}