Nativescript: application-settings: access all keys

Created on 23 Aug 2018  路  3Comments  路  Source: NativeScript/NativeScript

Did you verify this is a real problem by searching the [NativeScript Forum]

Yes: https://stackoverflow.com/questions/39582576/in-application-settings-iterate-the-key-values

Tell us about the problem

Right now there seems to be no way to get a representation of all key-value pairs that are currently stored in the application-settings. So it is not possible to say "delete all data for keys that starts with 'data-2018-08-21-...' " because we do not know which keys are stored and we like to iterate through all the keys to check which ones should be removed.

On native side, there seems to be a way to achieve this:
https://developer.android.com/reference/android/content/SharedPreferences#getAll()
https://developer.apple.com/documentation/foundation/nsuserdefaults/1415919-dictionaryrepresentation?language=objc

Which platform(s) does your issue occur on?

Both

enhancement good first issue hacktoberfest

Most helpful comment

@felix-idf here is a basic solution for getting all the keys on both Android and iOS

TypeScript

import { getNativeApplication } from "application";
import { isAndroid, isIOS } from "platform";
import * as utils from "utils/utils";

export function getAllKeys() {
    if (isAndroid) {
        let sharedPreferences = (<android.app.Application>getNativeApplication()).getApplicationContext().getSharedPreferences("prefs.db", 0);
        let mappedPreferences = sharedPreferences.getAll();
        let iterator = mappedPreferences.keySet().iterator();

        while (iterator.hasNext()) {
            let key = iterator.next();
            // console.log(key); // myString, myNumbver, isReal
            let value = mappedPreferences.get(key);
            // console.log(value); // "John Doe", 42, true
        }

    } else if (isIOS) {
        var userDefaults = utils.ios.getter(NSUserDefaults, NSUserDefaults.standardUserDefaults);
        let dictionaryUserDefaults = userDefaults.dictionaryRepresentation();

        let allKeys = dictionaryUserDefaults.allKeys;
        let allValues = dictionaryUserDefaults.allValues;
        // console.log(dictionaryUserDefaults.allKeys);
        // console.log(dictionaryUserDefaults.allValues);
    }
}

All 3 comments

@felix-idf here is a basic solution for getting all the keys on both Android and iOS

TypeScript

import { getNativeApplication } from "application";
import { isAndroid, isIOS } from "platform";
import * as utils from "utils/utils";

export function getAllKeys() {
    if (isAndroid) {
        let sharedPreferences = (<android.app.Application>getNativeApplication()).getApplicationContext().getSharedPreferences("prefs.db", 0);
        let mappedPreferences = sharedPreferences.getAll();
        let iterator = mappedPreferences.keySet().iterator();

        while (iterator.hasNext()) {
            let key = iterator.next();
            // console.log(key); // myString, myNumbver, isReal
            let value = mappedPreferences.get(key);
            // console.log(value); // "John Doe", 42, true
        }

    } else if (isIOS) {
        var userDefaults = utils.ios.getter(NSUserDefaults, NSUserDefaults.standardUserDefaults);
        let dictionaryUserDefaults = userDefaults.dictionaryRepresentation();

        let allKeys = dictionaryUserDefaults.allKeys;
        let allValues = dictionaryUserDefaults.allValues;
        // console.log(dictionaryUserDefaults.allKeys);
        // console.log(dictionaryUserDefaults.allValues);
    }
}

A getAllKeys() sounds like a nice contribution for the application-settings module.

This issue is a good candidate to get involved in the framework and submit a PR. You can check out the contributing guide for more info on how to do that.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings