Rezaul Karim
Rezaul Karim

Reputation: 1349

How to convert realm list to RealmResults<Object>?

I wanted to query on realm list as:

City cityObj = realmCitylist.where().equalTo("name", strCity).findFirst();

but error run time occurs as:

UnsupportedOperationException: This method is only available in managed mode

So, i googled and find this question with answer: This method is only available in managed mode, which suggested that it's not possible to query on realm list.

So i have to query on RealmResults as:

RealmResults< City > realmCities;
City cityObj = realmCities.where().equalTo("name", strCity).findFirst();

Question: How can i convert reamList to RealmResults?

Upvotes: 1

Views: 2135

Answers (1)

EpicPandaForce
EpicPandaForce

Reputation: 81588

This method is only available in managed mode

This means that you can only use this method if it belongs to a RealmList inside a managed RealmObject.

So this doesn't work:

RealmList<Blah> blahs = new RealmList<>();
blahs.where()... // <-- this is an unmanaged list, this won't work

Solution: if you want to query on top of a RealmList, it should belong to a managed RealmObject, returned by a Realm instance.

RealmList<Blah> blahs = realm.where(Doh.class).findFirst().getBlahs();
RealmResults<Blah> blahResults = blahs.where().equalTo(BlahFields.NAME, strCity).findAll();

In your case, you probably just want to execute a RealmQuery towards a Realm instance in the first place.

RealmResults<City> cities = realm.where(City.class).findAll();
City city = cities.where().equalTo(CityFields.NAME, strCity).findFirst();

Upvotes: 1

Related Questions