Query in the Realm database: find all objects containing the query string

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) { /* Begin Query */ RealmQuery<T> query = realmDatabase.where(clazz).beginGroup(); Field[] fields = clazz.getDeclaredFields(); for (int i = 0; i < fields.length; i++) { /* Skip NON-String Values */ 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 All Objects Found */ return query.endGroup().findAll(); } 
+6
source share
2 answers

Kingdom Christian is here. The reason your code doesn't work is because contains() only works for String fields ( http://realm.io/docs/java/api/io/realm/RealmQuery.html#contains-java.lang .String-java.lang.String- ), so when you try to use contains() it fails with an integer field.

If you want to search for different types of data, you will need to relate them to those, for example:

 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++) { Class<?> type = fields[i].getType(); String fieldName = fields[i].getName(); if (i > 0) { query.or() } if (type.equals(String.class)) { query.contains(fieldName, searchQuery) } else if (type.equals(Integer.class)) { query.equalTo(fieldName, Integer.parseInt(searchQuery)) } else { ... } } /* Return All Objects Found */ return query.endGroup().findAll(); } 
+8
source

Check if you can use this code. I think you can cover all the primitives. I have added code for String, int and float.

 import java.lang.reflect.Field; public class ReflectionTest { String stringValue; int intValue; float floatValue; public static void main(String[] args) { Field[] fields = ReflectionTest.class.getDeclaredFields(); for (Field field : fields) { if (field.getType().toString().contains("int")) { System.out.println("int Name \t" + field.getName()); } if (field.getType().toString().contains("float")) { System.out.println("float Name \t" + field.getName()); } if (field.getType().toString().contains("String")) { System.out.println("String Name \t" + field.getName()); } } } } 
0
source

Source: https://habr.com/ru/post/985645/


All Articles