Realm objects can only be accessed on the thread they where created.

435 views
Skip to first unread message

Joe Scott

unread,
Nov 26, 2014, 8:30:30 PM11/26/14
to realm...@googlegroups.com
private void updateOrInsertToLog(Message msg)
    {

        RealmQuery<RealmMessagesLog> query = realm.where(RealmMessagesLog.class);
        query.equalTo("PacketID", msg.getPacketID());
        RealmResults<RealmMessagesLog> result = query.findAll();
        if(result.size() > 0)
        {
            realm.beginTransaction();
            result.get(0).setPacketID(msg.getPacketID());
            result.get(0).setType(msg.getType().toString());
            result.get(0).setSubject(msg.getSubject());
            result.get(0).setBody(msg.getBody());
            realm.commitTransaction();
        }
        else
        {
            realm.beginTransaction();
            RealmMessagesLog msg_log = realm.createObject(RealmMessagesLog.class);
            msg_log.setBody(msg.getBody());
            msg_log.setSubject(msg.getSubject());
            msg_log.setType(msg.getType().toString());
            msg_log.setPacketID(msg.getPacketID());
            realm.commitTransaction();
        }
    }

@Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        accManager = new AccountManager(this);
        realm =  Realm.getInstance(this);
        BusProvider.getInstance().register(this);
        connectToXMPPServer();
        return Service.START_STICKY;
    }

Hi, i'm trying to use Realm in a service, i simply want to read OR write to the database, but i'm constantly given

Realm objects can only be accessed on the thread they where created.

I haven't MADE any objects to access, it wont let me! Not to mention, if it DID create those objects, it would have been on the thread i'm already on surely? I saw some example on the GitHub that seemed to have something to do with services, but involved complex additional classes and extending 'KillableThread' etc?

Would love to get some help on this, thanks!

Christian Melchior

unread,
Nov 27, 2014, 4:56:04 AM11/27/14
to Joe Scott, realm...@googlegroups.com
Hi Joe

There shouldn't be any problem in using Realm in a Service. A Service is really just an extension of the UI thread, and doesn't use its own thread unless you start one there.

It is a little hard to see from you code sample, but be aware that you cannot access a Realm instance across threads either, ie. you have to call Realm.getInstance() on each thread you are working on. From you sample I cannot see from where your updateOrInsertToLog() method is called, but it uses a class variable called realm. If this is shared across threads, it might be the root of your problems.

I can see why the error message might be a bit confusing in that regard, and could probably use a rewording.

Let me know if it helped?

--
Christian Melchior
Senior Android Developer


--
You received this message because you are subscribed to the Google Groups "Realm Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to realm-java+...@googlegroups.com.
To post to this group, send email to realm...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/realm-java/b48e439d-a28c-4d40-b5a5-5edc91e4281e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



{#HS:57287027-72#}

Joe Scott

unread,
Nov 27, 2014, 6:44:46 AM11/27/14
to realm...@googlegroups.com, br...@live.co.uk, he...@realm.io
Hi Christian,

Thank you for the response! As far as i'm aware i'm not creating ANY threads, here is the initialization

 private Realm realm;
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        accManager = new AccountManager(this);
        realm =  Realm.getInstance(this);
        BusProvider.getInstance().register(this);
        connectToXMPPServer();
        return Service.START_STICKY;
    }

Unfortunately i also tried using it as a local variable, placing it immediately above the calls to realm, still nothing. I'm probably mistaken somewhere?

Christian Melchior

unread,
Nov 27, 2014, 6:59:06 AM11/27/14
to Joe Scott, realm...@googlegroups.com
Hi Joe

Yes, from a quick glance it looks alright. Without more code, it is hard to tell. Are you in a position to share the code? You can always send it to he...@realm.io for full confidentiality.


--
Christian Melchior
Senior Android Developer


{#HS:57287027-72#}

Joe Scott

unread,
Nov 27, 2014, 6:59:33 AM11/27/14
to realm...@googlegroups.com, br...@live.co.uk, he...@realm.io
Damn it! After a night to sleep on it and another look, it was very obvious, turns out the ASmack method for 'processPacket' where the function was being called was actually on a separate thread. All fixed, thanks!

Christian Melchior

unread,
Nov 27, 2014, 7:22:03 AM11/27/14
to Joe Scott, realm...@googlegroups.com
No problem.

Good luck with the project and let us know if you have trouble/feedback.


--
Christian Melchior
Senior Android Developer


On Thu, Nov 27, 2014 at 11:59 AM UTC, Joe Scott <br...@live.co.uk> wrote:
Damn it! After a night to sleep on it and another look, it was very obvious, turns out the ASmack method for 'processPacket' where the function was being called was actually on a separate thread. All fixed, thanks!



On Thu, Nov 27, 2014 at 11:59 AM UTC, Joe Scott <br...@live.co.uk> wrote:
Damn it! After a night to sleep on it and another look, it was very
obvious, turns out the ASmack method for 'processPacket' where the function
was being called was actually on a separate thread. All fixed, thanks!



On Thu, Nov 27, 2014 at 11:58 AM UTC, Help <he...@realm.io> wrote:
Hi Joe

Yes, from a quick glance it looks alright. Without more code, it is hard to tell. Are you in a position to share the code? You can always send it to he...@realm.io for full confidentiality.

{#HS:57287027-72#}
Reply all
Reply to author
Forward
0 new messages