从rx和dagger中的错误线程进行领域访问 [英] Realm access from incorrect thread in rx and dagger

查看:67
本文介绍了从rx和dagger中的错误线程进行领域访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题要问很多,但是我被阅读这个问题感到困惑.在我的项目中,我以mvp模式使用dagger,rx和realm.我有一个带有LoginActivity的应用程序,当用户正确登录时,来自服务器的信息存储在此页面的领域中,并且用户跳至MainActivity.在MainActivity中,我需要此用户信息,但出现此错误:

I know this question asks a lot but i am confused by reading this question.In my project i am using dagger, rx and realm in mvp pattern.I have an application with a LoginActivity that when the user login correctly, the user information that comes from server store in realm in this page and user jumps to MainActivity.In MainActivity i want this user info but i got this error:

java.lang.IllegalStateException:来自错误线程的领域访问.只能在创建对象的线程上访问领域对象.

java.lang.IllegalStateException: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

我知道:

来自错误线程的领域访问.只能在创建对象的线程上访问领域对象.

Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.

在我的Appliction类中,我初始化了DaggerAplicationComponnet.我有applicationModule:

In my Appliction class i have initialized DaggerAplicationComponnet.I have applicationModule:

@AppScope
@Component(modules = {ApplicationModule.class, NetworkModule.class , AppRealmModule.class})
public interface ApplicationComponent {
    Realm getRealm();
    ...
}

和AppRealmModule:

and AppRealmModule:

@Module
public class AppRealmModule {

    @Provides
    @AppScope
    Realm provideRealm (Context context) {
        Realm.init(context);
        RealmConfiguration realmConfiguration = new RealmConfiguration.Builder()
                .deleteRealmIfMigrationNeeded()
                .name("Payeshdb")
                .build();
        Realm.setDefaultConfiguration(realmConfiguration);
        return Realm.getDefaultInstance();
    }

这意味着,我在ui thread上创建了领域实例.现在在MainActivity中,我已经阅读了领域:

That means , i create realm instance on ui thread.Now in MainActivity i have read realm:

@Override
public Observable<User> findUser() {
    return Observable.create(new ObservableOnSubscribe<User>() {
        @Override
        public void subscribe(ObservableEmitter<User> emitter) throws Exception {
            try {
                User user = realm.where(User.class).findFirst();
                User user1 = realm.copyFromRealm(user);
                emitter.onNext(user1);
                emitter.onComplete();

            }catch (Exception e) {
                emitter.onError(e);
            }
        }
    });
}

现在,在演示者中,我以这种方式使用rx:

Now, in presenter i am using rx in this way:

userRepository.findUser()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DisposableObserver<User>() {
                    @Override
                    public void onNext(User user) {

                    }

                    @Override
                    public void onError(Throwable e) {
                      // got error: Realm access from incorrect thread.
                    }

                    @Override
                    public void onComplete() {

                    }
                });

这是我遇到错误的地方.在某些帖子中,我读过这篇文章:

this is a place that i got above error. In some post i have read this:

Use copyFromRealm to get the data from realm which will return them as plain objects rather than realm objects.

我做到了,但是我仍然出错.

And i do that but i still got error.

是否可以用匕首初始化领域并使用不同的线程进行粗体操作?

Is it possible initialize realm with dagger and use different thread for crud operation?

您的建议是什么?

推荐答案

@Override
public Observable<User> findUser() {
  return Observable.create(new ObservableOnSubscribe<User>() {
    @Override
    public void subscribe(ObservableEmitter<User> emitter) throws Exception {
        try(Realm realm = Realm.getDefaultInstance()) {
            realm.refresh();
            User user = realm.where(User.class).findFirst();
            User user1 = realm.copyFromRealm(user);
            emitter.onNext(user1);
            emitter.onComplete();
        } catch (Exception e) {
            emitter.onError(e);
        }
    }
  });
}

这篇关于从rx和dagger中的错误线程进行领域访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆