当前位置:网站首页>Jetpack use exception problem collection
Jetpack use exception problem collection
2022-08-11 06:34:00 【rest of my life love static】
livedata
1、问题
LiveDataupdate data errorjava.lang.IllegalStateException: Cannot invoke setValue on a background thread
解决方案:
setValue(T) 必须在主线程中调用 , 而 postValue(T) 既可以在主线程中调用, 也可以在子线程中调用.
viewmodel
1、在使用kotlin语言创建ViewModelFactory的时候,An exception will be thrown
解决方案:
在kotlinOptions中添加如下内容
kotlinOptions {
jvmTarget = '1.8'
freeCompilerArgs += [
"-Xjvm-default=all",
]
}
Hilt
在老项目中,After adding the relevant dependencies according to the official documentation,An exception occurs
官方文档
编译异常
解决方案:
1、Add the following plugin dependencies
apply plugin: 'kotlin-android'
2、Be sure to put the above plugin dependencies first
//hilt
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
Preferences DataStore
public class PreferenceManager {
private RxDataStore<Preferences> dataStore;
private PreferenceManager() {
//配置dataStore
dataStore = new RxPreferenceDataStoreBuilder(AppGlobalUtils.getApplication(), /*name=*/ "settings").build();
}
private static class HOLDER {
private static PreferenceManager INSTANCE = new PreferenceManager();
}
public static PreferenceManager getInstance() {
return HOLDER.INSTANCE;
}
/**
* 保存int类型的数据
* @param key
* @param value
*/
public void putInt(String key, int value) {
dataStore.updateDataAsync(prefsIn -> {
MutablePreferences mutablePreferences = prefsIn.toMutablePreferences();
mutablePreferences.set(PreferencesKeys.intKey(key), value);
return Single.just(mutablePreferences);
});
}
/**
* 获取int类型的数据
* @param key
* @return
*/
public Flowable<Integer> getInt(String key) {
Preferences.Key<Integer> EXAMPLE_COUNTER = PreferencesKeys.intKey(key);
Flowable<Integer> exampleCounterFlow =
dataStore.data().map(prefs -> prefs.get(EXAMPLE_COUNTER));
return exampleCounterFlow;
}
}
问题:
当获取数据的时候,使用Flowable.subscribe(Subscriber<? super T> s)方法,在onNext(Integer integer)There is no callback to this method.
PreferenceManager.getInstance().getInt(KEY)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Integer>() {
@Override
public void onSubscribe(Subscription s) {
}
@Override
public void onNext(Integer integer) {
Word word = new Word(String.valueOf(integer));
mWordViewModel.insert(word);
value++;
}
@Override
public void onError(Throwable t) {
}
@Override
public void onComplete() {
}
});
解决方式1(异步):
在onSubscribe(Subscription s) 回调中调用s.request(1)方法.
为什么一定要调用s.request()方法呢?Combine the source code comments to understand:
No events will be sent by a Publisher until demand is signaled via this method
Before signaling of demand via this method,The publisher will not send any events.
解决方式2(异步):
调用subscribe(Consumer<? super T> onNext)
PreferenceManager.getInstance().getInt(KEY)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Word word = new Word(String.valueOf(integer));
mWordViewModel.insert(word);
value++;
}
});
解决方式3(同步):
调用blockingFirst()
Integer integer = PreferenceManager.getInstance().getInt(KEY).blockingFirst();
Word word = new Word(String.valueOf(integer));
mWordViewModel.insert(word);
value++;
边栏推荐
猜你喜欢
随机推荐
Tinker接入全流程---编译篇
Compilation exception resolution
Day 70
谨此留个纪念
[Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow
C language implementation guess Numbers (with source code, can be directly run)
promise.all 学习(多个promise对象回调)
构建面向特征工程的数据生态 ——拥抱开源生态,OpenMLDB全面打通MLOps生态工具链
2021-09-11 C language variables and memory allocation
Promise 中状态改变和回调执行先后顺序 和promise多次回调
Promise.race学习(判断多个promise对象执行最快的一个)
Day 76
net6的Web MVC项目实现限流功能
vscode插件开发——懒人专用markdown插件开发
Interpretation of the paper: Cross-Modality Fusion Transformer for Multispectral Object Detection
Node stepping on the pit 80 port is occupied
端口的作用
NUC980-开发环境搭建
OpenMLDB Pulsar Connector:高效打通实时数据到特征工程
OpenMLDB官网升级,神秘贡献者地图带你快速进阶








