问题已解决
通过将 realm.commitTransaction();
替换为 realm.copyToRealm(item);
解决问题
问题
当我通过监听器作为接口(interface)
从对话框获取数据并尝试将所选数据保存为类模型时。我无法在表中创建新行
从对话框获取数据:
takeUserEwalletCurrency.setOnTakeUserEwallet(new TakeUserEwalletCurrency.IOnChooseEwallet() {
@Override
public void getItem(int item) {
takeUserEwalletCurrency.dismissDialog();
switch (item) {
case 1:
String choiceUSDUserEwallet = "SHUSD" + userPhoneNumber.substring(1, userPhoneNumber.length());
updateEwalletsHistory(choiceUSDUserEwallet, "SHUSD");
to_ewallet.setText(choiceUSDUserEwallet);
validateEnteredEwalletNumber();
break;
case 2:
...
break;
}
}
});
takeUserEwalletCurrency.showDialog();
创建一个新行:
private void updateEwalletsHistory(final String currentEwallet, final String ewalletCurrency) {
final EwalletsHistory exists_row = realm.where(EwalletsHistory.class).equalTo("ewalletName", currentEwallet).findFirst();
if (exists_row == null) {
realm.executeTransaction(new Realm.Transaction() {
@Override
public void execute(Realm realm) {
EwalletsHistory item = new EwalletsHistory();
item.setEwalletName(currentEwallet);
item.setEwalletCurrency(ewalletCurrency);
realm.commitTransaction();
}
});
}
}
对话框界面和在表格中创建新行等都是相同的 Activity
最佳答案
这是因为您在 executeTransaction()
内调用 commitTransaction()
。
executeTransaction()
处理 begin/commit/cancelTransaction()
方法调用。
关于java - Android Realm 获取不在事务内错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38969777/