I'm trying to create a method for querying a table in Realm with the option for excluding certain rows (by their IDs). Since Realm doesn't support multiple equal parameters similar to its findAllSorted(String[], boolean[]) method, I'm performing it in a loop.
Is there a better way to do this job?
public static RealmResults<MessageItem> getAll(long threadId, Collection<Long> excludedMessageIds) {
final Realm realm = Realm.getDefaultInstance();
RealmQuery<MessageItem> allMessagesQuery = realm.where(MessageItem.class);
// Apply conditions
// 1. Thread to look into
if (threadId != -1)
allMessagesQuery = allMessagesQuery.equalTo(MessageItem.COLUMN_THREAD_ID, threadId);
// 2. Message IDs to exclude
for (final Long excludedMessageId : excludedMessageIds) {
allMessagesQuery = allMessagesQuery.notEqualTo(
MessageItem.COLUMN_MESSAGE_ID,
excludedMessageId
);
}
return allMessagesQuery.findAll();
}
There is no such method like that. Probably you could try to add field like private boolean excluded and set it to true where you exclude your messages. Then query all like this
return realm.where(MessageItem.class)
.equalTo(MessageItem.COLUMN_THREAD_ID, threadId);
.equalTo(MessageItem.COLUMN_EXCLUDED, false);
.findAll();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With