简体   繁体   中英

Save single object to file system

I want to know how to save single object(for example user's token) to file system. Is shared preferences best practices? Or it be better to use table with single row or something else?

If your user token is session based (change at every application launch) then it would be better to save in application cache .

If your user token not change as per application life-cycle, then shared preference is best option.

Shared preference is recommended for saving value pair

For details reference please refer this link

Shared prefrences will be the best choice, https://github.com/Pixplicity/EasyPrefs here is a library to make it easier for you aswell

For single entries like token etc, Shared Preferences is the best. following is the sample code to save it:

(Kotlin)

val value = "this-is-my-token"
 val preferences = PreferenceManager.getDefaultSharedPreferences(applicationContext ?: return)
        val editor = preferences.edit()
        editor.putString("token", value).apply()

(Java)

Editor editor = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).edit();
editor.putString("token", "my-token-value").commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM