java - Android Realm 获取不在事务内错误

标签 java android

问题已解决

通过将 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/

相关文章:

java - `AnnotationConfigNonEmbeddedWebApplicationContext` 还没有刷新

java - 空 Toast 消息

android - ListView 位置 onStart

java - Realm .copyToRealmOrUpdate() 导致出现重复项

java - 如何将一个jsp文件的数据使用到另一个jsp文件中

java - 如何在 Spring Batch 4 中获取 ItemReader 中的 JobExecutionId

java - 单击在 selenium 驱动程序 java 中不起作用

java.lang.ClassNotFoundException : javax. el.ELContext

android - 文档页面中的相机示例在 android 2.3.x 上失败

Android ICS 模拟器相机