【问题标题】:realm access from icorrect thread even if I get another instance即使我得到另一个实例,来自错误线程的领域访问
【发布时间】:2016-06-22 10:49:13
【问题描述】:

我正在尝试检索一些数据并在 AsyncTask 中更新它们。 问题是我得到一个
Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created.
错误,即使我在异步任务中更新我的领域实例。
这是我的代码:

 new AsyncTask<Void, Void, Void>(){
            @Override
            protected Void doInBackground(Void... params) {
                mRealm = Realm.getDefaultInstance();
                // my queries & updates
                return null;
            }
 }.execute();

【问题讨论】:

  • 请发布堆栈跟踪抱怨的代码并指出问题所在的行。

标签: android multithreading android-asynctask realm


【解决方案1】:

尝试在onPreExecute() 方法中创建默认实例。

new AsyncTask<Void, Void, Void>(){
            private Realm mRealm;
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                mRealm = Realm.getDefaultInstance();
            }

            @Override
            protected Void doInBackground(Void... params) {
                // my queries & updates
                return null;
            }
        }.execute();

希望对你有帮助。

【讨论】:

  • 您能解释一下为什么这会解决问题吗?
猜你喜欢
  • 2017-03-13
  • 1970-01-01
  • 2017-09-01
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多