Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Your Realm is opened from a thread without a Looper

Tags:

android

realm

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() {
            @Override
            public void execute(Realm backgroundRealm) {
                long id = customerPrimaryKey.incrementAndGet();
                customer.setId(id);
                backgroundRealm.copyToRealm(customer);
            }
        }, new Realm.Transaction.OnSuccess() {
            @Override
            public void onSuccess() {
                Log.d(LOG_TAG, "Customer Added");
            }
        }, new Realm.Transaction.OnError() {
            @Override
            public void onError(Throwable error) {
                Log.d(LOG_TAG, error.getMessage());
            }
        });

        insertRealm.close();

    }

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.

Update - Fixed It turns out that there is nothing wrong with the query, problem is that I was calling it from IntentService. I was trying to seed the database on app first run, so I fixed this like this:

protected void onHandleIntent(Intent intent) {
        Realm realm = Realm.getDefaultInstance();

        //Add sample Customers to database
        List<Customer> customers = SampleCustomerData.getCustomers();

        realm.beginTransaction();
        for (Customer customer: customers){
            customer.setId(customerPrimaryKey.getAndIncrement());
            realm.copyToRealm(customer);
        }
        realm.commitTransaction();

realm.close();

    }

That fixed, outside of the IntentService, the query works fine when called from a UI Thread.

like image 729
Val Okafor Avatar asked Nov 05 '16 05:11

Val Okafor


2 Answers

In order for

insertRealm.executeTransactionAsync(new Realm.Transaction() {
      @Override
        public void execute(Realm backgroundRealm) {
            //...
        }
    }, new Realm.Transaction.OnSuccess() {
        @Override
        public void onSuccess() {
            // !!!
        }
    }, new Realm.Transaction.OnError() {
        @Override
        public void onError(Throwable error) {
            // !!!
        }
    });

asynchronous transaction callbacks to be called on a background thread, the thread needs to be associated with a Looper (and therefore have Handlers that can communicate with the thread with that Looper).


Solution, use synchronous transaction on background thread.

But you've already figured that out.

protected void onHandleIntent(Intent intent) {
    Realm realm = null;
    try {
        realm = Realm.getDefaultInstance();
        final List<Customer> customers = SampleCustomerData.getCustomers();
        realm.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {
                for (Customer customer: customers){
                    customer.setId(customerPrimaryKey.getAndIncrement());
                    realm.insert(customer);
                }
            }
        });
    } finally {
        if(realm != null) {
            realm.close();
        }
    }
}
like image 141
EpicPandaForce Avatar answered Nov 03 '22 15:11

EpicPandaForce


Realm relies on Android' thread communication by Hanfler and Looper classes. It looks like you query async operation from another background Thread (why?, it is already in background, use synchronous version). To fix that you need thread with Active Looper. Use Handler thread as you background thread - it will have Looper initialized

like image 2
Alex Shutov Avatar answered Nov 03 '22 15:11

Alex Shutov