mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
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:
committed by
Svetoslav
parent
634cf9a822
commit
1615061591
@@ -102,6 +102,16 @@ export var testClear = function () {
|
||||
TKUnit.assert(!appSettings.hasKey(numberKey), "Failed to remove key: " + numberKey);
|
||||
};
|
||||
|
||||
export var testFlush = function () {
|
||||
appSettings.setString(stringKey, "String value");
|
||||
// >> application-settings-flush
|
||||
var flushed = appSettings.flush();
|
||||
// will return boolean indicating whether flush to disk was successful
|
||||
// << application-settings-flush
|
||||
TKUnit.assert(flushed, "Flush failed: "+ flushed);
|
||||
TKUnit.assert(appSettings.hasKey(stringKey), "There is no key: " + stringKey);
|
||||
};
|
||||
|
||||
export var testInvalidKey = function () {
|
||||
try {
|
||||
appSettings.hasKey(undefined);
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user