[英]Realm Query Returns Incomplete Results Set In Android
I have been having a very unusual behavior when using Realm
to insert
and read/query
data. 使用
Realm
insert
和read/query
数据时,我一直有非常不正常的行为。 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
autoIncrementingId
since I have not found a version of Realm
that offers this out of the box! 我有以下代码来处理
autoIncrementingId
因为我还没有找到可以提供此功能的Realm
版本!
CashOrder cashOrder = new CashOrder();
realm.beginTransaction();
int lastCashOrderId;
RealmResults<CashOrder> cashOrders = realm.where(CashOrder.class).findAll();
cashOrders.sort("id", Sort.ASCENDING);
if (cashOrders.isEmpty()){
cashOrder.setId("0");
}else{
lastCashOrderId = Integer.parseInt(cashOrders.last().getId());
cashOrder.setId(String.valueOf(lastCashOrderId + 1));
}
//the rest of the code here
//then copyToRealm here;
realm.copyToRealm(cashOrder);
realm.commitTransaction();
The problem 问题
Insertion of data into the database
works just fine but the moment the id value reaches 10
- implying 11 items in the table since my id starts at (0)
, I get a Realm Primary Key Exception
which basically complains that I am trying to add an already existing object. 将数据插入
database
工作正常,但是当id值达到10
时(0)
因为ID从(0)
开始,意味着表中有11个项目(0)
,我收到了Realm Primary Key Exception
,该Realm Primary Key Exception
基本上是在抱怨我试图添加一个已经存在的对象。
I am explicitly calling realm.copyToRealm(obj)
instead of realm.copyToRealmOrUpdate(obj)
because that is what I want to do. 我明确地调用
realm.copyToRealm(obj)
而不是realm.copyToRealmOrUpdate(obj)
因为这是我想要做的。 Updates only are allowed when doing an Edit . 仅在执行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!
提前致谢!
It's because your ID is a STRING
and therefore it's ordered as 1
, 10
, 2
, 3
, ... 9
. 这是因为你的ID是一个
STRING
,因此它的排序为1
, 10
, 2
, 3
,... 9
。
This means when you evaluate the "next ID" when you had already inserted "10"
, 这意味着当您已经插入
"10"
时评估“下一个ID”时,
lastCashOrderId = Integer.parseInt(cashOrders.last().getId());
lastCashOrderId
will be 9, and you'll be inserting 10
again. lastCashOrderId
将为9,并且您将再次插入10
。
Solution: use long
id for longs, or re-think your auto-increment IDs (f.ex. use UUID.randomString()
or something) 解决方案:将
long
id用作long,或者重新考虑您的自动增量ID(例如,使用UUID.randomString()
或其他名称)
PS you should use findAllSorted()
instead of findAll().sort()
for consistent behavior when you update to the newer Realm versions. PS,当您更新到较新的Realm版本时,应使用
findAllSorted()
而不是findAll().sort()
以获得一致的行为。
EDIT: please use long newId = realm.where(CashOrder.class).max("age").longValue() + 1;
编辑:请使用
long newId = realm.where(CashOrder.class).max("age").longValue() + 1;
to evaluate the new ID, and do this while you're in a transaction. 评估新ID,并在进行交易时执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.