简体   繁体   English

从 Preferences DataStore 读取返回 null

[英]Reading from Preferences DataStore returns null

My DataStore keeps returning null even though I've set a default value on the preferences manager using the elvis operator.即使我使用 elvis 运算符在首选项管理器上设置了默认值,我的 DataStore 也一直返回 null。 Also, my edit function to set a preference on a key-value pair isn't being called so I'm not even sure my datastore is properly setup in general.此外,我在键值对上设置首选项的编辑功能没有被调用,所以我什至不确定我的数据存储是否正确设置。 I'm pretty sure the class is properly injected though, because I can see it as a variable while using breakpoints.不过,我很确定该类已正确注入,因为我可以在使用断点时将其视为变量。

Basically val countryCode = viewModel.countrySettings.value on the ViewModel always returns null基本上val countryCode = viewModel.countrySettings.value在 ViewModel 上总是返回 null

PreferencesManager class PreferencesManager 类

 
const val TAG = "PreferencesManager"
 
const val DEFAULT_COUNTRY_PREFERENCE = "us"
const val DEFAULT_CATEGORY_PREFERENCE = "general"
 
private val Context.dataStore by preferencesDataStore(name = PREFERENCES_NAME)
 
@Singleton
class PreferencesManager @Inject constructor(@ApplicationContext appContext: Context) {
 
    private val preferencesDataStore = appContext.dataStore
 
    //Pairs are separated but I'll create an appropriate data class later.
    val countrySettings = preferencesDataStore.data
        .catch { exception ->
            if (exception is IOException) {
                Log.e(TAG, "Error while trying to read user preferences", exception)
                emit(emptyPreferences())
            } else {
                throw exception
            }
        }
        .map { preference ->
            val country = preference[PreferenceKeys.COUNTRY] ?: DEFAULT_COUNTRY_PREFERENCE
            country
        }
 
    val categorySettings = preferencesDataStore.data
        .catch { exception ->
            if (exception is IOException) {
                Log.e(TAG, "Error while trying to read user preferences", exception)
                emit(emptyPreferences())
            } else {
                throw exception
            }
        }
        .map { preferences ->
            val category = preferences[PreferenceKeys.CATEGORY] ?: DEFAULT_CATEGORY_PREFERENCE
            category
        }
 
    suspend fun setCountryPreference(country: String) {
        preferencesDataStore.edit { preference ->
            preference[PreferenceKeys.COUNTRY] = country
        }
    }
 
    suspend fun setCategoryPreference(category: String) {
        preferencesDataStore.edit { preference ->
            preference[PreferenceKeys.CATEGORY] = category
        }
    }
 
 
    private object PreferenceKeys {
        val COUNTRY = stringPreferencesKey("country")
        val CATEGORY = stringPreferencesKey("category")
    }
 
}

ViewModel视图模型

class MainViewModel @Inject constructor(
    private val repository: Repository,
    private val preferencesManager: PreferencesManager
): ViewModel() {
 
 
    val countrySettings = preferencesManager.countrySettings.asLiveData()
    val categorySettings = preferencesManager.categorySettings.asLiveData()
 
/* .... */
 
    fun setCountryPreference(country: String) {
        viewModelScope.launch {
            preferencesManager.setCountryPreference(country)
        }
    }
 
    fun setCategoryPreference(category: String) {
        viewModelScope.launch {
            preferencesManager.setCategoryPreference(category)
        }
    }
}

Fragment分段

 
    val viewModel: MainViewModel by activityViewModels()
 
    private var _binding: FragmentSettingsCountryScreenBinding? = null
    private val binding get() = _binding!!
 
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
 
        _binding = FragmentSettingsCountryScreenBinding.bind(view)
 
        //Using breakpoints I've noticed this function isn't even called on the preferences manager to set the value, which is weird
        viewModel.setCountryPreference("us")
 
        val countryCode = viewModel.countrySettings.value
 
        binding.radiogroup.check(adaptPreferenceFromDataStore(countryCode!!))
 
 
        binding.radiogroup.setOnCheckedChangeListener { _, checkedId ->
            viewModel.setCountryPreference(adaptPreferenceToDataStore(checkedId))
        }
 
    }
 
    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}

Yes, ianhanniballake is correct, it was a newbie mistake - I completely forgot I had to observe the livedata value and just then set the UI parameters.是的,ianhanniballake 是正确的,这是一个新手错误 - 我完全忘记了我必须观察 livedata 值然后才设置 UI 参数。 I was trying to set the preferences based on the value of a few switches (and vice-versa).我试图根据几个开关的值设置首选项(反之亦然)。 Here's the proper function for setting it up:这是设置它的正确功能:

fun setupSwitches() {
        viewModel.countrySettings.observe(viewLifecycleOwner, { preference ->
            binding.radioGroup.check(adaptPreferenceFromDataStore(preference))
        })

        binding.radioGroup.setOnCheckedChangeListener { _, checkedId ->
            viewModel.setCountryPreference(adaptPreferenceToDataStore(checkedId))
        }
    }

Then called setupSwitches() on onViewCreated .然后在onViewCreated上调用setupSwitches()

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

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