realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

RealmResults returns partial Results

Open echirchir opened this issue 9 years ago • 12 comments

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!

echirchir avatar Aug 24 '17 12:08 echirchir

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?

cmelchior avatar Aug 24 '17 12:08 cmelchior

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

echirchir avatar Aug 24 '17 12:08 echirchir

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.

cmelchior avatar Aug 24 '17 12:08 cmelchior

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.

cmelchior avatar Aug 24 '17 12:08 cmelchior

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.

echirchir avatar Aug 24 '17 12:08 echirchir

You kinda need to specify what thread you are on, whether you are closing Realm instances properly, etc

Zhuinden avatar Aug 24 '17 12:08 Zhuinden

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.

cmelchior avatar Aug 24 '17 12:08 cmelchior

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.

echirchir avatar Aug 24 '17 12:08 echirchir

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 avatar Aug 24 '17 12:08 cmelchior

@cmelchior Let me give realm.refresh() a try then I will report back asap! Thanks for your help guys;

echirchir avatar Aug 24 '17 12:08 echirchir

@echirchir hope your problem gets solved. I am closing this issue now. Please let us know if you still need help on this.

beeender avatar Sep 05 '17 09:09 beeender

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!

echirchir avatar Sep 05 '17 11:09 echirchir