Kkk.
Kkk.

Reputation: 1

Java android Realm access from incorrect thread

Hi I create a Realm on onCreate() next I want to in public class CheckerThread extends AsyncTask<Void, String, Boolean> in doInBackground store a date but I have a

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

onCreate I have this :

realm = RealmController.with(this).getRealm();

next in AsynTask I download a data and I want to save this , I want to do this in doInBackground , but when I do this I have :

Caused by: java.lang.IllegalStateException: Realm access from incorrect thread. Realm instance can only be closed on the thread it was created.

I do on doInBackground this but it doesn't help :

try {
                                    realm = RealmController.with(getApplication()).getRealm();
                                    RealmController.with(getApplication()).save(data);
                                }
                                finally {
                                    realm.close();
                                }

A RealmController ;

public class RealmController {

    private static RealmController instance;
    private final Realm realm;

    public RealmController(Application application) {
        realm = Realm.getDefaultInstance();
    }

    public static RealmController with(Fragment fragment) {

        if (instance == null) {
            instance = new RealmController(fragment.getActivity().getApplication());
        }
        return instance;
    }

    public static RealmController with(Activity activity) {

        if (instance == null) {
            instance = new RealmController(activity.getApplication());
        }
        return instance;
    }

    public static RealmController with(Application application) {

        if (instance == null) {
            instance = new RealmController(application);
        }
        return instance;
    }

    public static RealmController getInstance() {

        return instance;
    }

    public Realm getRealm() {

        return realm;
    }

    //Refresh the realm istance
    public void refresh() {

        realm.refresh();
    }

Upvotes: 1

Views: 3044

Answers (2)

EpicPandaForce
EpicPandaForce

Reputation: 81539

As also mentioned here, don't access managed RealmObjects queried on the UI thread inside doInBackground() which runs on a background thread. Open and close the Realm instance with try-finally inside doInBackground() and requery your object based on its primary key.

Also, throw out that RealmController, it is a pointless addition based on the badly-written Ravi Tamada infohive Realm tutorial, which tells you to use Realm 0.82.2 despite that version being 2 years old. It completely disregards the fact that Realm instances are thread-confined, and you'll run into IllegalStateException with it, just like you did now. Ravi Tamada got away with it because he executes all his write transactions on the UI thread, which is generally a bad idea.

Let me state in bold caps, THE INFOHIVE REALM TUTORIAL IS TERRIBLE AND PROMOTES BAD PRACTICES. DO NOT USE.

You should refer to this repository for a proper Realm tutorial. But you can also check my profile for additional Realm-related resources.

Upvotes: 5

Kamal Oli
Kamal Oli

Reputation: 55

It seems like you are trying to access an object created on main thread. AsyncTask gives you a method that is onPostExecution(), from here you can access your realm object but not in doInBackground() method. In cas you want to access the object from doInBackground() then you can use Handler class. Docs is given in link below.enter link description here

Upvotes: -1

Related Questions