Android:从不正确的线程访问领域.Realm 对象只能在创建它们的线程上访问 [英] Android: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created

查看:28
本文介绍了Android:从不正确的线程访问领域.Realm 对象只能在创建它们的线程上访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此在 IntentService 中,应用程序可能处于活动状态或非活动状态,onHandleIntent 被调用,我将其放置在代码下方.这是我将数据存储到领域的地方.

So inside a IntentService, the app maybe active or inactive , onHandleIntent gets called , where I have placed this below code.This is where I store the data into realm.

 Realm realm = null;
    try { 
        realm = Realm.getDefaultInstance();
        realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {

                for (AppItem item : items) {
                    AppItem item2 = realm.createObject(AppItem.class, UUID.randomUUID().toString());
                    item2.mName = item.mName;
                    item2.mCount = item.mCount;
                    item2.mUsageTime = item.mUsageTime;
                }
            }
        });
    } finally {
        if (realm != null) {
            realm.close();
        }
    }

然后我试图在 AsyncTask 中的 onPostExecute 中访问它,在 doInBackground 中,我得到了 RealmResults,然后将其存储到 List 并将其发送到放置此代码的 onPostExecute.appItems 这里是 ReamObject

Then I am trying to access it in onPostExecute in AsyncTask, In doInBackground, I am getting the RealmResults<AppItem>, then storing it into List <AppItem> and sending it to onPostExecute where this code is placed. appItems is ReamObject here

 Realm backgroundRealm = Realm.getDefaultInstance();
            backgroundRealm.executeTransactionAsync(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                    for (AppItem item : appItems) { 
 //getting error here   if (item.mUsageTime <= 0) continue;
                        mTotal += item.mUsageTime;
                        item.mCanOpen = mPackageManager.getLaunchIntentForPackage(item.mPackageName) != null;


               }
              }
            });

我都使用 executeTransactionAsync 完成了,但仍然出现以下错误.

Both I have done using executeTransactionAsync, still I get the following error.

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

推荐答案

根据错误消息:一旦您在 1 个线程上获得任何 Realm 对象,它们只能在该线程上访问.对其他线程的任何访问都会引发该异常.

As per the error message: Once you obtain any Realm objects on 1 thread, they can ONLY be accessed on that thread. Any access on other threads will throw that exception.

来自 AsyncTask 的doInBackground"在后台线程上运行.onPostExecute"在 UI 线程上运行.所以在这里你在后台线程上获得 Realm 对象,并尝试在 UI 线程上访问它们 => 异常.

"doInBackground" from AsyncTask is run on a background thread. "onPostExecute" is run on the UI thread. So here you get Realm objects on a background thread, and try to access them on the UI thread => Exception.

您应该在后台线程上执行所有操作,或者在 UI 线程上执行所有操作.

You should either do everything on the background thread, or everything on the UI thread.

如果你正在做一个非常复杂的查询,我建议在 RealmQuery 上使用findAllAsync",因为这将在后台线程上运行查询,并将它们移动到主线程,但它由 Realm 在内部处理一种安全的方式.

If you're doing a very complex query, i suggest using "findAllAsync" on the RealmQuery, as this will run the query on a background thread, and move them over to the main thread, but it's handled internally by Realm in a safe manner.

这篇关于Android:从不正确的线程访问领域.Realm 对象只能在创建它们的线程上访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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