简体   繁体   English

如何只使用一次 sharedpref.edit()

[英]How to use sharedpref.edit() only once

I have a code where I called sharedPref.edit() and sharedPref.apply() multiple times.我有一个代码,我多次调用了 sharedPref.edit() 和 sharedPref.apply() 。 How to make convert it to call only once.如何将其转换为仅调用一次。

if (success) {
                                val data = response.getJSONObject("data")
                                sharedPreferences.edit().putBoolean("isLoggedIn", true).apply()
                                sharedPreferences.edit()
                                    .putString("user_id", data.getString("user_id")).apply()
                                sharedPreferences.edit().putString("name", data.getString("name"))
                                    .apply()
                                sharedPreferences.edit().putString("email", data.getString("email"))
                                    .apply()
                                sharedPreferences.edit()
                                    .putString("mobile_number", data.getString("mobile_number"))
                                    .apply()
                                sharedPreferences.edit()
                                    .putString("address", data.getString("address")).apply()

                                StyleableToast.Builder(this)
                                    .text("Welcome " + data.getString("name"))
                                    .backgroundColor(Color.RED)
                                    .textColor(Color.WHITE).show()

                                userSuccessfullyLoggedIn()
                            }

I want to use the method call only once.我只想使用方法调用一次。

This can be called once, the returned editor instance can be stored in a variable and re-used.这可以调用一次,返回的编辑器实例可以存储在变量中并重新使用。

How to do this ??这个怎么做 ??

These little steps will organize your code.这些小步骤将组织您的代码。

You can put it like this:你可以这样写:

val editor =  sharedPreferences.edit()  

Then use it :然后使用它:

editor.putBoolean("isLoggedIn", true)

And Add others values without ".apply()"并在没有“.apply()”的情况下添加其他值

Then Put at the End:然后放在最后:

 editor.apply()

you can create your custom Shared Preferences您可以创建自定义共享首选项

class CustomSharedPreferences {

    companion object {

        private val PREFERENCES_USER_NAME = "preferences_user_name"
        private var sharedPreferences: SharedPreferences? = null

        @Volatile private var instance: CustomSharedPreferences? = null

        private val lock = Any()
        operator fun invoke(context: Context) : CustomSharedPreferences = instance ?: synchronized(lock){
            instance ?: makeCustomSharedPreferences(context).also {
                instance = it
            }
        }

        private fun makeCustomSharedPreferences(context: Context) : CustomSharedPreferences{
            sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context)
            return CustomSharedPreferences()
        }
    }

    fun saveUser(name: String, email: String){
        sharedPreferences?.edit(commit = true){
            putString(PREFERENCES_USER_NAME, name)
        }
    }

    fun getUser() = sharedPreferences?.getString(PREFERENCES_USER_NAME, "")

}

You can save all information to SP in saveUser().您可以在 saveUser() 中将所有信息保存到 SP。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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