I have a collection of objects of a different type that are stored in a Realm database. Now I want to request a specific type of objects, including all fields of this object. The method I wrote below gets all the declared fields of the Object and iterates in the fields to check if the given query string is contained.
It works fine for String field types, but throws java.lang.IllegalArgumentException: Field 'documentCompletionStatus': type mismatch. Was STRING, expected INTEGER. java.lang.IllegalArgumentException: Field 'documentCompletionStatus': type mismatch. Was STRING, expected INTEGER. for integer values ββsince my search query object is a string. I am currently skipping Non-String values ββas a workaround, but I'm curious if it can be searched in all fields.
For example, if a user wants to find an integer value aimed at the "Age" field of Objects, I cannot get it to work this way.
public <T extends RealmObject> List<T> searchDatabase(Class<T> clazz, String searchQuery) { RealmQuery<T> query = realmDatabase.where(clazz).beginGroup(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { if (!fields[i].getType().equals(String.class)) { continue; } if (i == 0) { query.contains(fields[i].getName(), searchQuery, false); } else { query.or().contains(fields[i].getName(), searchQuery, false); } } return query.endGroup().findAll(); }
source share