Android如何在线程上使用Realm

问题描述 投票:2回答:4

在我的应用程序中,我阅读保存在手机上的联系人,这可能需要很长时间,然后我把它放在Thread嵌套我使用Realm但我收到此错误:

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

我的解决方案无法解决此问题,例如:

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

要么

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

在嵌套线程上,

java android realm
4个回答
4
投票

您需要在给定线程上尝试使用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();
            }
        }
    }
});

虽然您不应该在UI线程上进行同步写入。如果您在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();
                }
            });
    }
});

我个人更喜欢创建一个单线程执行程序,Realm写入完成。

 private final Executor executor = Executors.newSingleThreadExecutor();

 ...

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

对于UI线程,您通常已经有一个由onCreate/onDestroyonStart/onStop打开/关闭的Realm实例。


3
投票

问题不在于你在不同的线程中使用Realm,问题是你在不同的线程中使用了真实的instance。通常这是一个简单的修复,类似的应该做:

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

0
投票

您现在可以使用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");
        });

请注意,在execute(Realm realm)方法中,您需要将此领域对象用于所有事务,而不是全局领域对象。


0
投票

在任何线程中使用try with resources。您不需要关闭realm实例。它会自动关闭。

try (Realm r = Realm.getDefaultInstance()) {
    r.executeTransaction(realm -> realm.insertOrUpdate(mData));
}
© www.soinside.com 2019 - 2024. All rights reserved.