Reputation: 221
Is there a way to find all the classes that exist in the Realm DB?
Example, say you had a Realm database that existed of Dogs, Cats, and Birds. They all have a name property.
You want to search for any object that has a name of "Joe".
Obv you can say
realm.Where(Dog.class).Contains()
But can you do this dynamically?
something like:
realm = Realm.getInstance();
List<?> realmObjects = realm.GetAllClassesThatExtendRealmObject();
for(Object obj : realmObjects)
{
//search or what not here
}
Thanks
Upvotes: 0
Views: 840
Reputation: 20126
That is unfortunately not possible with Realm as all query results has to be of the same type. Your best bet is making keeping a list of RealmTypes and let them all implement a interface like so
public interface Named {
public String getName();
public void setName(String name);
}
public class Foo extends RealmObject implements Named {
private String name;
...
}
public class Bar extends RealmObject implements Named {
}
List<Class<? extends RealmObject?>> types = Arrays.asList(Foo.class, Bar.class);
List<Named> namedResults = new ArrayList<>();
for (Class type : types) {
List<Named> results = (List<Named>) realm.where(type).equalTo("name", "Joe");
namedResults.addAll(results);
}
However doing that means you loose one of the main benefits of Realm, namely that RealmResults are lazy loaded.
Upvotes: 0