Android:来自错误线程的领域访问.只能在创建对象的线程上访问领域对象 [英] Android: Realm access from incorrect thread. Realm objects can only be accessed on the thread they were created

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

问题描述

因此在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();
        }
    }

然后我试图在AsyncTaskonPostExecute中访问它,在doInBackground中,我得到了RealmResults<AppItem>,然后将其存储到List <AppItem>中并将其发送到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.

"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:来自错误线程的领域访问.只能在创建对象的线程上访问领域对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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