当前位置:网站首页>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++;
原网站

版权声明
本文为[rest of my life love static]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/223/202208110515269785.html