这里是你能做什么 - 考虑境界读作无份,这意味着它不贵,也不会影响你的UI线程多,存储您的物品后,就可以findAllSorted("createdAt)
或findAllSorted("id")
,然后找到使用last()
最后一个ID方法是这样的:
//after commitTransaction,
RealmResults<Transactions> allTransactions = realm.where(Transactions.class).findAllSorted("createdAt");
//If you have an incrementing id column, do this
long lastInsertedId = allTransactions.last().getId();
这应该会给你最后插入给定模型的ID。
还有一点很重要的一点,为了这个工作,你必须在你的模型中有一个像这样的id列;
public class Transactions extends RealmObject{
@PrimaryKey @Index
private long id;
//getters and setters accordingly!
}
我希望这有助于!祝你好运,快乐的编码!
UPDATE
我刚刚意识到realm.copyToRealm(obj)
返回一个对象!
这意味着你可以简单地这样做:
realm.beginTransaction();
Transactions transaction = realm.copyToRealm(newTransaction);
long id = transaction.getId();
realm.commitTransaction();
请试试这个,让我知道!
我学会了在字符串中隐藏整数的难题,这是一个糟糕的主意!使用多久 – Eenvincible