我正在努力让自己熟悉DataStore
,所以在我当前的项目中,我正在尝试使用它。
在我的依赖中。我已经添加 :
implementation "androidx.datastore:datastore-preferences:1.0.0-alpha06"
然后我创建了这个类来处理数据存储:
class BasicDataStore(context: Context) :
PrefsDataStore(
context,
PREF_FILE_BASIC
),
BasicImpl {
override val serviceRunning: Flow<Boolean>
get() = dataStore.data.map { preferences ->
preferences[SERVICE_RUNNING_KEY] ?: false
}
override suspend fun setServiceRunningToStore(serviceRunning: Boolean) {
dataStore.edit { preferences ->
preferences[SERVICE_RUNNING_KEY] = serviceRunning
}
}
companion object {
private const val PREF_FILE_BASIC = "basic_preference"
private val SERVICE_RUNNING_KEY = booleanPreferencesKey("service_running")
}
}
@Singleton
interface BasicImpl {
val serviceRunning: Flow<Boolean>
suspend fun setServiceRunningToStore(serviceRunning: Boolean)
}
在我的视图模型中,我正在尝试使用该值,如下所示:
class MainViewModel(application: Application) : AndroidViewModel(application) {
...
private val basicDataStore = BasicDataStore(application)
val serviceRunning
: StateFlow<Boolean> get()
= basicDataStore.serviceRunning as StateFlow<Boolean>
fun setServiceRunning(serviceRunning: Boolean) {
viewModelScope.launch(IO) {
basicDataStore.setServiceRunningToStore(serviceRunning)
}
}
}
但它给了我以下错误:
Caused by: java.lang.ClassCastException: com.mua.roti.data.datastore.BasicDataStore$serviceRunning$$inlined$map$1 cannot be cast to kotlinx.coroutines.flow.StateFlow
at com.mua.roti.viewmodel.MainViewModel.getServiceRunning(MainViewModel.kt:33)
...
在 xml 中,在 UI 部分:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@{main.serviceRunning.value.toString()}" />
使用 viewmodel,一切都变得如此酷炫和简单,易于阅读和实现。现在我对Flow感到困惑。谢谢。