Val Okafor Val Okafor - 2 months ago 56
Android Question

Your Realm is opened from a thread without a Looper

I thought I was following the recommended Realm approach for running Async data inserts like this:

public void addCustomer(final Customer customer) {
Realm insertRealm = Realm.getDefaultInstance();
insertRealm.executeTransactionAsync(new Realm.Transaction() {
public void execute(Realm backgroundRealm) {
long id = customerPrimaryKey.incrementAndGet();
}, new Realm.Transaction.OnSuccess() {
public void onSuccess() {
Log.d(LOG_TAG, "Customer Added");
}, new Realm.Transaction.OnError() {
public void onError(Throwable error) {
Log.d(LOG_TAG, error.getMessage());



However, when I run the above code I get "Your Realm is opened from a thread without a Looper and you provided a callback, we need a Handler to invoke your callback"

I am running this code in a non-Activity class, what I am doing wrong here and how can I fix it. Thanks.


You need to call addCustomer from UI thread.