我正在从Realm读取/写入这个异常@H_502_1@
@H_502_1@
06-19 09:49:26.352 11404-11404/****** E/ContentValues: loadData: one rror Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
at io.realm.BaseRealm.checkIfValID(BaseRealm.java:385)
at io.realm.RealmResults.isLoaded(RealmResults.java:115)
at io.realm.OrderedRealmCollectionImpl.size(OrderedRealmCollectionImpl.java:307)
at io.realm.RealmResults.size(RealmResults.java:60)
at java.util.AbstractCollection.isEmpty(AbstractCollection.java:86)
at /****** .lambda$loadData$0(SplashPresenter.java:42)
at /****** $$Lambda$1.test(UnkNown Source)
at io.reactivex.internal.operators.observable.ObservableFilter$FilterObserver.onNext(ObservableFilter.java:45)
at io.reactivex.observers.Serializedobserver.onNext(Serializedobserver.java:111)
at io.reactivex.internal.operators.observable.ObservableDelay$DelayObserver$1.run(ObservableDelay.java:84)
at io.reactivex.internal.schedulers.ScheduledRunnable.run(ScheduledRunnable.java:59)
at io.reactivex.internal.schedulers.ScheduledRunnable.call(ScheduledRunnable.java:51)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:272)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)@H_502_1@
这是代码:@H_502_1@
@H_502_1@
mSubscribe = Observable.just(readData()) .delay(DELAY, TimeUnit.SECONDS) .filter(value -> !value.isEmpty()) .switchIfEmpty(createRequest()) .subscribeOn(Schedulers.io()) .observeOn(AndroIDSchedulers.mainThread()).subscribe(data -> { getVIEw().hIDeLoading(); writeData(data); }, (throwable -> { }));
读数据@H_502_1@
@H_502_1@
private List<categoryModel> readData() { Realm defaultInstance = Realm.getDefaultInstance(); List<categoryModel> Title = defaultInstance.where(categoryModel.class).findAllSorted("Title"); defaultInstance.close(); return Title;}
写数据@H_502_1@
@H_502_1@
private voID writeData(List<categoryModel> categoryModels) { try { Realm defaultInstance = Realm.getDefaultInstance(); defaultInstance.executeTransactionAsync(realm -> realm.insertOrUpdate(categoryModels)); defaultInstance.close(); } finally { getVIEw().notifyActivity(categoryModels); } }
如何使用正确的线程遵循此逻辑?@H_502_1@
解决方法:@H_502_1@
您只能在事务中 *** 作Realm对象,或者只能在读/写这些对象的线程中 *** 作.在您的情况下,您从readData方法获取RealmResult并使用RxJava切换导致异常的线程.使用copyFromrealm从realm获取数据,它将它们作为普通对象而不是realm对象返回.@H_502_1@ 总结
以上是内存溢出为你收集整理的android – Rxjava Realm从错误的线程访问全部内容,希望文章能够帮你解决android – Rxjava Realm从错误的线程访问所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)