Application-Settings: Non-blocking changes (#5091)

* fix(app-settings): changes are now non-blocking

* chore(app-settings): return result of flush to disk

* chore(app-settings): added test for flush
This commit is contained in:
Daniel Freiling
2017-12-05 22:38:18 +01:00
committed by Svetoslav
parent 634cf9a822
commit 1615061591
4 changed files with 30 additions and 10 deletions

View File

@@ -1,7 +1,7 @@
import * as common from "./application-settings-common";
import { getNativeApplication } from "../application";
var sharedPreferences;
var sharedPreferences: android.content.ISharedPreferences;
function ensureSharedPreferences() {
if (!sharedPreferences) {
sharedPreferences = (<android.app.Application>getNativeApplication()).getApplicationContext().getSharedPreferences("prefs.db", 0);
@@ -49,7 +49,7 @@ export function setBoolean(key: string, value: boolean): void {
common.ensureValidValue(value, "boolean");
var editor = sharedPreferences.edit();
editor.putBoolean(key, value);
editor.commit();
editor.apply();
}
export function setString(key: string, value: string): void {
@@ -57,7 +57,7 @@ export function setString(key: string, value: string): void {
common.ensureValidValue(value, "string");
var editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
editor.apply();
}
export function setNumber(key: string, value: number): void {
@@ -65,17 +65,21 @@ export function setNumber(key: string, value: number): void {
common.ensureValidValue(value, "number");
var editor = sharedPreferences.edit();
editor.putFloat(key, float(value));
editor.commit();
editor.apply();
}
export function remove(key: string): void {
verify(key);
var editor = sharedPreferences.edit();
editor.remove(key);
editor.commit();
editor.apply();
}
export function clear(): void {
ensureSharedPreferences();
sharedPreferences.edit().clear().commit();
sharedPreferences.edit().clear().apply();
}
export var flush = function (): boolean {
return sharedPreferences.edit().commit();
}

View File

@@ -61,3 +61,9 @@ export function remove(key: string): void;
* Removes all values.
*/
export function clear(): void;
/**
* Flush all changes to disk synchronously.
* @return {boolean} flag indicating if changes were saved successfully to disk.
*/
export function flush(): boolean;

View File

@@ -39,29 +39,29 @@ export var setBoolean = function (key: string, value: boolean): void {
Common.checkKey(key);
Common.ensureValidValue(value, "boolean");
userDefaults.setBoolForKey(value, key);
userDefaults.synchronize();
}
export var setString = function (key: string, value: string): void {
Common.checkKey(key);
Common.ensureValidValue(value, "string");
userDefaults.setObjectForKey(value, key);
userDefaults.synchronize();
}
export var setNumber = function (key: string, value: number): void {
Common.checkKey(key);
Common.ensureValidValue(value, "number");
userDefaults.setDoubleForKey(value, key);
userDefaults.synchronize();
}
export var remove = function (key: string): void {
Common.checkKey(key);
userDefaults.removeObjectForKey(key);
userDefaults.synchronize();
}
export var clear = function (): void {
userDefaults.removePersistentDomainForName(utils.ios.getter(NSBundle, NSBundle.mainBundle).bundleIdentifier);
}
export var flush = function (): boolean {
return userDefaults.synchronize();
}