0

This is My RealmObject:

public class Channel extends RealmObject{

    private String id = "";
    private String name = "";
...

I am trying to construct a query where each string in a list of string contains (not equal) to the "name" field. I am aware I can use the in() method with a ArrayList, but the in() method validate if the string EQUALS and rather need CONTAINS.

I have tried the following:

  1. realm.where(Channel.class).rawPredicate("name CONTAINS[c] $0",list.toArray()).findAll() This perform the query only on the first String in "list"
  2. realm.where(Channel.class).in("name", list.toArray()).findAll() is performing there search only if the field "name" EQUALS to any of the Strings in the list, where I need it to search if the field CONTAINS any of the values in the List of Strings

Any ideas of how to use a method just like in() but that will compare if the string equals or contain the field value?

Thanks

ia244
  • 145
  • 1
  • 10
  • The code and the question do not match; there is no - *List of string* - in the question - do you mean a List of Channel objects? Or... something else? Please take a moment and review [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Please update the QUESTION for clarity and add relevant code, and we'll take a look! – Jay Feb 22 '22 at 18:00
  • Hey @Jay I think you misunderstood my question or I wrote it wrong, the list of Strings is the query to search, so the list will be constructed by different names and any Channel object with a name that match or contain any of the strings in the list is the query I am trying to achieve – ia244 Feb 22 '22 at 19:49
  • let me say this back to you; You have Channel objects stored in Realm whose name property would contain a name "Jay", "Cindy", "Larry" etc. You want to query those object for any name that contains the letter "J" or "L". Right? – Jay Feb 22 '22 at 23:03
  • That's right, i have edited my question with the full examples i already tried – ia244 Feb 23 '22 at 00:53
  • We need a bit more clarity as the question still doesn't show any kind of List or array of objects. Please include the object which contains the List of `Channel` (or whatever it is) objects - there may be a very simple solution but we need to see that code. – Jay Feb 23 '22 at 14:56
  • You just need an OR query here; in Swift it would be `let results = realm.objects(PersonClass.self).where { $0.name.contains("J") || $0.name.contains("C") }` to find names with J or C in them. If you need something dynamic you could build the query in code, looping over each letter you want to query for and adding it to your query string. – Jay Feb 24 '22 at 19:40
  • The issue is that I have a list of strings, the list is quite dynamic so for one query it can be 10 strings in the list and for the other it can be only 2. Unfortunately I can't dynamically add/remove or() depending on the number of strings in the list (or can i?) – ia244 Feb 24 '22 at 20:22
  • Sure you can. You can build complex queries pretty easily in Realm. In Swift they are called NSPredicate's and those can actually be rolled into one query (filter) using an NSCompoundPredicate. Not sure what the android equivalent is but it can certainly be done and 10 strings is easy. OR you can, as mentioned, append 10 OR queries together. You could also add a function and List to your object; the function would take the name and break it down into separate components (each letter in name) and store each in the List. Then, you can use `containsAny:in` on the List object – Jay Feb 24 '22 at 22:49
  • Do you have any documentations for the containsAny:in? – ia244 Feb 25 '22 at 01:11
  • As mentioned we are Swift so here's the link to [contains:in](https://docs.mongodb.com/realm/sdk/swift/examples/filter-data/#collections). I am sure there's a corresponding function in Java or whatever you're coding in. Take a look at [Chaining](https://docs.mongodb.com/realm/sdk/java/examples/filter-data/#chain-queries) and something that's really on point is in the [Realm Query](https://docs.mongodb.com/realm/sdk/java/examples/filter-data/#query-with-realm-query-language) and in the example in the section with the comment `// Multiple predicates` as that's pretty much what you want to do. – Jay Feb 25 '22 at 18:33

1 Answers1

0

try this

RealmQuery<Channel> tasksQuery = realm.where(Channel.class).beginGroup();
for(String name: names){
    tasksQuery.equalTo("name", name)
}
tasksQuery.endGroup().findAll()
Zen
  • 1