У меня есть один класс RealmObject, и моя цель - добавить новые строки. Вот мой класс RealmObject
public class ZAccountTable extends RealmObject implements Serializable {
@Index
@PrimaryKey
private long id;
private String zAccountIdentificator="";
private Date openDate;
private Date closeDate;
private long amountInPeriod;
private long vatInPerion;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getzAccountIdentificator() {
return zAccountIdentificator;
}
public void setzAccountIdentificator(String zAccountIdentificator) {
this.zAccountIdentificator = zAccountIdentificator;
}
public Date getOpenDate() {
return openDate;
}
public void setOpenDate(Date openDate) {
this.openDate = openDate;
}
public Date getCloseDate() {
return closeDate;
}
public void setCloseDate(Date closeDate) {
this.closeDate = closeDate;
}
public long getAmountInPeriod() {
return amountInPeriod;
}
public void setAmountInPeriod(long amountInPeriod) {
this.amountInPeriod = amountInPeriod;
}
public long getVatInPerion() {
return vatInPerion;
}
public void setVatInPerion(long vatInPerion) {
this.vatInPerion = vatInPerion;
}
public long getOperationcounter() {
return operationcounter;
}
public void setOperationcounter(long operationcounter) {
this.operationcounter = operationcounter;
}
public long getAmount() {
return amount;
}
public void setAmount(long amount) {
this.amount = amount;
}
public long getVat() {
return vat;
}
public void setVat(long vat) {
this.vat = vat;
}
public long getOperationcounterAll() {
return operationcounterAll;
}
public void setOperationcounterAll(long operationcounterAll) {
this.operationcounterAll = operationcounterAll;
}
public boolean isSent() {
return isSent;
}
public void setSent(boolean sent) {
isSent = sent;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
private long operationcounter;
private long amount;
private long vat;
private long operationcounterAll;
private boolean isSent;
private String signature="";
}
Я могу добавить новую строку в мою таблицу следующим образом:
zAccountTable.setzAccountIdentificator("10");
Date openDate;
Date closeDate;
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm");
try {
openDate = formatter.parse("2018-04-30 15:01");
Log.e("Print result: ", String.valueOf(openDate));
closeDate = formatter.parse("2018-04-30 16:01");
zAccountTable.setOpenDate(openDate);
zAccountTable.setCloseDate(closeDate);
zAccountTable.setAmountInPeriod(10);
zAccountTable.setVatInPerion(0);
zAccountTable.setAmount(100);
zAccountTable.setVat(0);
zAccountTable.setOperationcounter(2);
zAccountTable.setSent(true);
zAccountTable.setSignature("signature");
CoreApplication.realm.executeTransaction(realm -> {
Number currentIdNum = realm.where(CashierTable.class).max("id");
int nextId;
if (currentIdNum == null) {
nextId = 1;
} else {
nextId = currentIdNum.intValue() + 1;
}
zAccountTable.setId(nextId);
realm.insertOrUpdate(zAccountTable);
});
} catch (ParseException e1) {
e1.printStackTrace();
}
Но у меня есть проблема, Иногда что-то не так в моем коде, я не могу добавить новую строку, Можно ли проверить, успешно ли добавлена новая строка, и отловить результат ошибки?