tux-world tux-world - 3 months ago 9
Android Question

Android Realm get Not inside a transaction Error

Problem Resolved



problem resolved by replace
realm.commitTransaction();
with
realm.copyToRealm(item);


Problem



When i get data from dialog by listener as
interface
and try to save chosen data as class model. i can not create new row in table

Get data from dialog:

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();


Create a new row:

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();
}
});
}
}


both of them such as Dialog interface and create new row in table are the same activity

Answer

It's because you're calling commitTransaction() inside executeTransaction().

executeTransaction() handles begin/commit/cancelTransaction() method calls.

Comments