当前位置:网站首页>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++;
边栏推荐
猜你喜欢

js学习进阶BOM部分(pink老师笔记)

JVM调优整理

vscode插件开发——代码提示、代码补全、代码分析(续)

mk文件介绍

Vscode remote connection server terminal zsh+Oh-my-zsh + Powerlevel10 + Autosuggestions + Autojump + Syntax-highlighting

论文解读TransFG: A Transformer Architecture for Fine-grained Recognition

USB 枚举过程中8 字节标准请求解析

STM32F407-浅~~析UART异步通信&USART_Init函数入口参数

STM32学习笔记(白话文理解版)—外部IO中断实验

Matplotlib找不到字体,打印乱码
随机推荐
ARM assembly instruction ADR and LDR
关于openlayer中swipe位置偏移的问题
STM32-中断优先级管理NVIC
The role of the port
OpenMLDB Meetup No.2 会议纪要
OpenMLDB + Jupyter Notebook: Quickly Build Machine Learning Applications
Day 68
[Meetup] OpenMLDBxDolphinScheduler engineering and scheduling link link characteristics, building the end-to-end MLOps workflow
哥德巴赫猜想与整数环
Manufacturer Push Platform-Huawei Access
OpenMLDB Pulsar Connector:高效打通实时数据到特征工程
js常用方法对象及属性
Day 73
Event Preview | On April 23, a number of wonderful sharing sessions of OpenMLDB will come, which will live up to the good time of the weekend
关于接口响应内容的解码
PAT乙级刷题之路
C语言实现猜数字(附带源码,可直接运行)
USB 枚举过程中8 字节标准请求解析
typescript学习日记,从基础到进阶(第二章)
使用ActiveReports制作第一张报表