Eenvincible Eenvincible - 1 year ago 49
Android Question

Realm Query Returns Incomplete Results Set In Android

I have been having a very unusual behavior when using

data. Here is what is happening to be precise:

Initial Status

When I start to insert data to the database, everything works fine. I have the following code to handle
since I have not found a version of
that offers this out of the box!

CashOrder cashOrder = new CashOrder();


int lastCashOrderId;
RealmResults<CashOrder> cashOrders = realm.where(CashOrder.class).findAll();

cashOrders.sort("id", Sort.ASCENDING);

if (cashOrders.isEmpty()){
lastCashOrderId = Integer.parseInt(cashOrders.last().getId());
cashOrder.setId(String.valueOf(lastCashOrderId + 1));

//the rest of the code here

//then copyToRealm here;


The problem

Insertion of data into the
works just fine but the moment the id value reaches
- implying 11 items in the table since my id starts at
, I get a
Realm Primary Key Exception
which basically complains that I am trying to add an already existing object.

I am explicitly calling
instead of
because that is what I want to do. Updates only are allowed when doing an Edit.

I am stuck here and I can't seem to figure out what is causing this issue!

More information

I am using:

compile 'io.realm:realm-android:0.87.4'

I will truly appreciate your help on this! Thanks in advance!

Answer Source

It's because your ID is a STRING and therefore it's ordered as 1, 10, 2, 3, ... 9.

This means when you evaluate the "next ID" when you had already inserted "10",

lastCashOrderId = Integer.parseInt(cashOrders.last().getId()); 

lastCashOrderId will be 9, and you'll be inserting 10 again.

Solution: use long id for longs, or re-think your auto-increment IDs (f.ex. use UUID.randomString() or something)

P.S. you should use findAllSorted() instead of findAll().sort() for consistent behavior when you update to the newer Realm versions.

EDIT: please use long newId = realm.where(CashOrder.class).max("age").longValue() + 1; to evaluate the new ID, and do this while you're in a transaction.