微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数据存储未从Flow接收事件

如何解决数据存储未从Flow接收事件

我正在使用datastore-preferences:1.0.0-alpha01,更新数据存储区值后似乎无法找回事件。我一直试图在片段和父活动中观察它,并得到相同的结果。创建DataStore实例并观察值流时,在声明观察者后立即收到一个事件(注册观察者后似乎很常见)。这将表明流程正在运行,并且观察者正在按照预期进行事件。然后,我在从不接收事件的同一观察者内部更新首选项的值。

我将本文用作参考文献https://medium.com/scalereal/hello-datastore-bye-sharedpreferences-android-f46c610b81d5,并与用来比较实现的仓库一起使用。显然,这是可行的。 https://github.com/PatilShreyas/DataStoreExample

DataStoreUtils

class DataStoreUtils(context: Context) {

    companion object {
        private const val TAG = "DataStoreUtils"
    }

    private val dataStore = context.createDataStore(name = Constants.PrefName.APP_PREFS)

    suspend fun setString(prefKey: String,value: String) {
        Log.d(TAG,"Setting $prefKey to $value")
        dataStore.edit { it[preferencesKey<String>(prefKey)] = value }
    }

    suspend fun getString(prefKey: String): String? {
        return dataStore.data.map { it[preferencesKey<String>(prefKey)] ?: return@map null }.first()
    }

    val usernameFlow: Flow<String?> = dataStore.data
        .catch {
            if (it is IOException) {
                it.printstacktrace()
                emit(emptyPreferences())
            }
            else { throw it }
        }
        .map { it[preferencesKey(Constants.SharedPrefKeys.USERNAME)] ?: return@map null }

}

UpdateConnectionDialog

class UpdateConnectionDialog : DialogFragment() {

    companion object {
        private const val TAG = "UpdateConnectionDialog"
    }

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        super.onViewCreated(view,savedInstanceState)

        save_btn.setonClickListener {
            runBlocking { authenticateResponse(ApiCalls.postLogin(body)!!) }
        }
    }

    override fun onResume() {
        super.onResume()

        dataStore ?: dataStore = DataStoreUtils(Application.appInstance?.applicationContext!!)

        dataStore.usernameFlow.asLiveData().observe(this) {
            Log.d(TAG,"Updated username to: $it")
        }
    }

    private fun authenticateResponse(response: ApiResponse<String>) {

        Log.d(TAG,"Auth request made.")

        when (response) {
            is ApiSuccessResponse<String> -> {
                AppLog.d(TAG,"Auth Success")

                lifecycleScope.launch {
                    dataStore.setString(Constants.PrefKeys.USERNAME,username_input.text.toString())
                    dataStore.setString(Constants.PrefKeys.PASSWORD,password_input.text.toString())
                }
            }
            is ApiErrorResponse<String> -> {
                AppLog.d(TAG,"Auth Failed")
            }
        }
    }
}

当视图膨胀时,这就是我在日志中看到的

D/UpdateConnectionDialog: onCreateView()
D/UpdateConnectionDialog: onViewCreated()
D/UpdateConnectionDialog: onResume()
D/UpdateConnectionDialog: Updated username to: X // Seems to be common with other examples I have found. After registering an observer it immediately triggers the event.
D/UpdateConnectionDialog: Auth request made.
D/UpdateConnectionDialog: Auth Success
D/DataStoreUtils: Setting USERNAME to X
D/DataStoreUtils: Setting PASSWORD to X

现在,在其他示例中,我们应该看到观察者因已更新而触发了一个事件,但我不是。在其他示例中,添加相同的日志记录点,显然观察者会收到更新事件。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。