RealmResults returns partial Results
I am having a very strange behavior while querying data from my Realm table.
Example
I have an object called OrderItem defined like this:
public class OrderItem extends RealmObject{
@PrimaryKey
@Index
private long id;
//the rest of the fields here
//then getters and setters as usual
}
The issue is quite unpredictable; because sometimes:
RealmResults<OrderItem> items = realm.where(OrderItem.class)
.equalTo("order_id", order_id)
.findAllSorted("id");
returns incomplete results - especially by 1 row;
This has been driving me nuts because I cannot exactly find out how it returns such partial results; doesn't matter how many rows I have in the said table!
NOTE
RealmVersion : classpath "io.realm:realm-gradle-plugin:3.3.1"
Thank you in advance!
Hi @echirchir I'm not sure what you mean by "incomplete results"? What is the expected output and how exactly does the output you see differ?
I want to get the whole data set that match that query criteria; so if items are 5 I should get 5 and not 4 or less;
I hope this clarifies my issue;
Thank you
How do you now that there are 5 items in the database? Without some more code and/or an example project that reproduces this it is hard to give any guidance or speculate what could be wrong.
Also, the query in the issue doesn't match the OrderItem class. order_id is not present. If you meant id, then it is a primary key, so that query should ever only ever return one element at most.
I use stetho in my development environment which allows me to inspect my database tables in android; so I can clearly see exact row size but when I query (I should mention that I query inside a service), it returns less sometimes and some other times, returns complete data set.
You kinda need to specify what thread you are on, whether you are closing Realm instances properly, etc
Most likely you are running the query on a thread that isn't fully updated yet. Realm is a MVCC database, so each thread has its own view of the dataset. I would recommend this article https://academy.realm.io/posts/threading-deep-dive/
You can either try to register a Changelistener on the query result, which should provide the correct result eventually or call realm.refresh() just before executing the query.
But sprinkling refresh() throughout the codebase is usually not a good idea.
Let me rephrase this:
I have a background service that queries items from a table for example:
OrderItem - contains line items for a given order, and each line item table has a column order_id which I later use to link which line items belong to which order as expected in queries.
I certainly close my realm once the service finishes its task.
You have a race condition in that case. If you open the Realm in the background service before the data is written, it will get a snapshot of the old data. If you really need to query on the latest data you need to call realm.refresh() as a background service is not a Looper thread, so it will not update automatically like e.g. the UI thread.
@cmelchior Let me give realm.refresh() a try then I will report back asap! Thanks for your help guys;
@echirchir hope your problem gets solved. I am closing this issue now. Please let us know if you still need help on this.
Thanks for this; I have used realm.refresh() for now and seems to solve that issue; am sure there is a better solution as pointed out in this thread regarding having several calls to this method!
Thank you for your help and tremendous support!