Skip to content

Realm Access From Incorrect Thread #1456

@slidenerd

Description

@slidenerd

Hi, I have read your documentation about using different Realm instances in different threads and how the objects realmlist and realmresults cannot be shared between threads, this is a completely different issue...
snap 2015-09-08 at 18 47 50

I have a class called FBGroup which basically extends RealmObject and looks like this
snap 2015-09-08 at 18 52 39

I am first loading it from the internet in the background thread and then storing it to Realm in the main thread, loading takes place with the help of Gson, my bg task doInBackground looks like this
snap 2015-09-08 at 18 54 09
It just has one line of code to load straight from JSON and the FBUtil method call looks like this
The highlighted line 111 is giving me the error that says Realm cannot be used in the bg thread, i am simply storing json data into the FBGroup object
snap 2015-09-08 at 18 55 22
There is no sign of Realm.getInstance or anything of that sort anywhere, so what is going on? Thanks for your suggestions and time in advance, does this have something to do with the fact that ID is a primary key and Realm is actually trying to read the key or something? I have added the default constructor to the FBGroup class, its not pasted here though

UPDATE 1
The debugger says this...for the single line inside the AsyncTask
snap 2015-09-08 at 19 07 15

UPDATE 2

I fixed the error by doing this, I made a copy of the object FBGroup and passed the copy to the AsyncTask and it works perfectly
snap 2015-09-08 at 20 27 23
Though I ll be honest , I dont understand the reason behind this line fixing the error, the object FBGroup comes from an ArrayList that is loaded like this
snap 2015-09-08 at 20 28 21

The method in consideration loads a list of Group objects from Realm database like this
snap 2015-09-08 at 20 29 28

It seems there is a problem here, I want to load a number of objects from Realm into an ArrayList, I think I have made some mistake here, your clarification would solve the issue, thanks in advance, in the meantime, let me try searching some solutions on realm to arraylist :)

UPDATE 3
This other technique worked as well where I directly duplicate stuff from the RealmResults to ArrayList, is there no way to go Realmresults to ArrayList in a more optimal way, I read your issues and saw a guy asking the reverse from List to RealmResults thanks, for your suggestions in advance
snap 2015-09-08 at 20 46 19

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions