LoginSignup
5
5

More than 5 years have passed since last update.

Realmで動的にqueryを追加する方法

Posted at

例えば、"qiita tomoima525"と検索したい場合、"qiita"と"tomoima525"を別クエリとして処理したいわけですが、その方法。

チェーンメソッドのorを使ってrealmのqueryに連結していくのがミソです。

realm = Realm.getInstance(context);
realmQuery = realm.where(column.class);

String query = "Qiita tomoima525";

String[] queryArray = query.split("[\\s]+");  //半角スペースも全角スペースも許容
for(int i = 0, size = queryArray.length; i < size ; i++){
    if(i !=0){
        realmQuery = realmQuery.or();
    }
    realmQuery = realmQuery.contains("column1",queryArray[i]);
}

realmQuery.findAll();
5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5