简体   繁体   English

Android如何在线程上使用Realm

[英]Android how can i use Realm on Thread

In my application I read contacts which saved on phone, this may be take a long time, then I put that on Thread nested that I'm using Realm but I get this error: 在我的应用程序中,我读取保存在手机上的联系人,这可能需要很长时间,然后我把它放在Thread嵌套我正在使用Realm但我收到此错误:

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

And my solutions don't resolve this problem such as : 我的解决方案无法解决此问题,例如:

new Handler().post(new Runnable() {
    @Override
    public void run() {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
            }
        });
    }
});

OR 要么

new Handler(getMainLooper()).post(new Runnable() {
    @Override
    public void run() {
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
            }
        });            
    }
});

on nested Thread, 在嵌套线程上,

You need an instance on the given thread where you're trying to use the Realm instance. 您需要在给定线程上尝试使用Realm实例的实例。

new Handler(Looper.getMainLooper()).post(new Runnable() { // <-- if you are not on UI thread and want to go there
    @Override
    public void run() {
        Realm realm = null;
        try {
            realm = Realm.getDefaultInstance();
            realm.executeTransaction(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                }
            });
        } finally {
            if(realm != null) {
                realm.close();
            }
        }
    }
});

Although you shouldn't be doing synchronous writes on the UI thread. 虽然您不应该在UI线程上进行同步写入。 Use async transaction instead if you write on the UI thread. 如果您在UI线程上书写,请使用异步事务。

new Handler(Looper.getMainLooper()).post(new Runnable() {  // <-- if you are not on UI thread and want to go there
    @Override
    public void run() {
          final Realm realm = Realm.getDefaultInstance();
          realm.executeTransactionAsync(new Realm.Transaction() {
                @Override
                public void execute(Realm realm) {
                }
            }, 
            new Realm.Transaction.OnSuccess() {
                @Override
                public void onSuccess() {
                    realm.close();
                }
            }, 
            new Realm.Transaction.OnError() {
                @Override
                public void onError(Throwable e) {
                    realm.close();
                }
            });
    }
});

I personally prefer creating a single-threaded executor, on which Realm writes are done. 我个人更喜欢创建一个单线程执行程序,Realm写入完成。

 private final Executor executor = Executors.newSingleThreadExecutor();

 ...

 executor.execute(() -> {
     try(Realm realm = Realm.getDefaultInstance()) {
         // use Realm on background thread
     }
 });

And for UI thread, you generally already have a Realm instance open/closed by either onCreate/onDestroy or onStart/onStop . 对于UI线程,您通常已经通过onCreate/onDestroyonStart/onStop打开/关闭了Realm实例。

The problem is not that you're using Realm in a different thread, the problem is that you're using the instance on the real in a different thread. 问题不在于你在不同的线程中使用Realm,问题是你在不同的线程中使用真实的instance Usually that's an easy fix, something like that should do: 通常这是一个简单的修复,类似的应该做:

Realm realmForThisThread = Realm.getDefaultInstance();
realmForThisThread.executeTransaction(new Realm.Transaction() {
    @Override
    public void execute(Realm realm) {
    }
}
...
realmForThisThread.close();

Use try with resources within any thread. 在任何线程中使用try with resources。 You don't need to close realm instance. 您不需要关闭realm实例。 It does close automatically. 它会自动关闭。

try (Realm r = Realm.getDefaultInstance()) {
    r.executeTransaction(realm -> realm.insertOrUpdate(mData));
}

You can now use Realm's very own asynchronous block to do the work. 您现在可以使用Realm自己的异步块来完成工作。

realm.executeTransactionAsync(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                // Some working is here.
            }
        }, new Realm.Transaction.OnSuccess() {
            // Some proceesing lines are here.
        }, new Realm.Transaction.OnError() {
            Log.d("AsyncTransaction", "ERROR");
        });

Be aware that, in the execute(Realm realm) method, you need to use this realm object for all the transactions, not your global realm object. 请注意,在execute(Realm realm)方法中,您需要将此领域对象用于所有事务,而不是全局领域对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM