Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Realm.io - How to use wildcard %LIKE% for a query

I'm trying to do:

mRealm
         .where(Contact.class)
         .equalTo(Contact.NAME, text, Case.INSENSITIVE)
         .findAllSortedAsync(Contact.NAME, Sort.ASCENDING);

Result: Expected result not met.

mRealm
         .where(Contact.class)
         .contains(Contact.NAME, text, Case.INSENSITIVE)
         .findAllSortedAsync(Contact.NAME, Sort.ASCENDING);

Result: Expected result not met.

Expected result:

mRealm
         .where(Contact.class)
         .like(Contact.NAME, text, Case.INSENSITIVE)
         .findAllSortedAsync(Contact.NAME, Sort.ASCENDING);
like image 233
charitha ratnayake Avatar asked Nov 05 '16 15:11

charitha ratnayake


3 Answers

NEW ANSWER:

Realm 2.3.0+:

public RealmQuery<E> like(String fieldName,
                      String value,
                      Case casing) 

Condition that the value of field matches with the specified substring, with wildcards:

  • '*' matches [0, n] unicode chars

  • '?' matches a single unicode char.

Parameters:

  • fieldName - the field to compare.

  • value - the wildcard string.

  • casing - how to handle casing. Setting this to Case.INSENSITIVE only works for Latin-1 characters.

Returns: the query object.

Throws: IllegalArgumentException - if one or more arguments do not match class or field type.


OLD ANSWER:

mRealm
     .where(Contact.class)
     .contains(Contact.NAME, text, Case.INSENSITIVE)
     .findAllSortedAsync(Contact.NAME, Sort.ASCENDING);

This should work, but you will receive a callback to an appended RealmChangeListener when the actual async query is completed.

A RealmRecyclerViewAdapter does this automatically from https://github.com/realm/realm-android-adapters.

like image 129
EpicPandaForce Avatar answered Nov 13 '22 11:11

EpicPandaForce


mRealm
     .where(Contact.class)
     .like(Contact.NAME, text, Case.INSENSITIVE)
     .findAllSortedAsync(Contact.NAME, Sort.ASCENDING);
like image 45
Raja Avatar answered Nov 13 '22 13:11

Raja


This piece of code works for me .

Realm realm=Realm.getDefaultinstance();
RealmResults<Item> reaaa = realm.where(Item.class).like("name",query,Case.INSENSITIVE).findAll();
like image 1
sanjay Avatar answered Nov 13 '22 13:11

sanjay