Customer class
@RealmClass
public class Customer extends RealmObject {
@PrimaryKey private int id;
private int customerSerial;
@Required private String customerName;
private RealmList<Invoice> invoices = new RealmList<>();
public Customer() {}
public Customer(int id, int customerSerial, String customerName) {
this.id = id;
this.customerSerial = customerSerial;
this.customerName = customerName;
}
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public int getCustomerSerial() { return customerSerial; }
public void setCustomerSerial(int customerSerial) { this.customerSerial = customerSerial; }
public String getCustomerName() { return customerName; }
public void setCustomerName(String customerName) { this.customerName = customerName; }
public List<Invoice> getInvoices() { return invoices; }
public void setInvoices(RealmList<Invoice> invoices) { this.invoices = invoices; }
}
Invoice class
@RealmClass
public class Invoice extends RealmObject {
@PrimaryKey private int invoiceNumber;
private Date invoiceDate;
private String invoiceTime;
private int debtorSerNo;
private int deliveryStatus;
private long invoiceSort;
private int driverId;
private String debtorName;
private boolean codCustomer;
private boolean outstandingCOD;
private double outstandingCODAmount;
private String deliveryNotes;
private int lineCount;
public Invoice() {}
public Invoice(TtInvoice invoice) {
this.invoiceNumber = invoice.getInvoiceNumber();
this.invoiceDate = invoice.getInvoiceDate();
this.invoiceTime = invoice.getInvoiceTime();
this.debtorSerNo = invoice.getDebtorSerNo();
this.deliveryStatus = 0;
this.debtorName = invoice.getDebtorName();
this.codCustomer = invoice.isCodCustomer();
this.outstandingCOD = invoice.isOutstandingCOD();
this.outstandingCODAmount = invoice.getOutstandingCODAmount();
this.deliveryNotes = invoice.getDeliveryNotes();
this.lineCount = invoice.getLineCount();
}
public int getInvoiceNumber() {
return invoiceNumber;
}
public void setInvoiceNumber(int invoiceNumber) {
this.invoiceNumber = invoiceNumber;
}
public Date getInvoiceDate() {
return invoiceDate;
}
public LocalDateTime getInvoiceDateTime() {
LocalTime time = LocalTime.parse(invoiceTime);
LocalDate date = invoiceDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return date.atTime(time);
}
public void setInvoiceDate(Date invoiceDate) {
this.invoiceDate = invoiceDate;
}
public String getInvoiceTime() {
return invoiceTime;
}
public void setInvoiceTime(String invoiceTime) {
this.invoiceTime = invoiceTime;
}
public int getDebtorSerNo() {
return debtorSerNo;
}
public void setDebtorSerNo(int debtorSerNo) {
this.debtorSerNo = debtorSerNo;
}
public String getDebtorName() {
return debtorName;
}
public void setDebtorName(String debtorName) {
this.debtorName = debtorName;
}
public boolean isCodCustomer() {
return codCustomer;
}
public void setCodCustomer(boolean codCustomer) {
this.codCustomer = codCustomer;
}
public boolean isOutstandingCOD() {
return outstandingCOD;
}
public void setOutstandingCOD(boolean outstandingCOD) {
this.outstandingCOD = outstandingCOD;
}
public double getOutstandingCODAmount() {
return outstandingCODAmount;
}
public void setOutstandingCODAmount(double outstandingCODAmount) {
this.outstandingCODAmount = outstandingCODAmount;
}
public String getDeliveryNotes() {
return deliveryNotes;
}
public void setDeliveryNotes(String deliveryNotes) {
this.deliveryNotes = deliveryNotes;
}
public int getLineCount() {
return lineCount;
}
public void setLineCount(int lineCount) {
this.lineCount = lineCount;
}
public int getDeliveryStatus() {
return deliveryStatus;
}
public void setDeliveryStatus(int deliveryStatus) {
this.deliveryStatus = deliveryStatus;
}
public long getInvoiceSort() {
return invoiceSort;
}
public void setInvoiceSort(long invoiceSort) {
this.invoiceSort = invoiceSort;
}
public int getDriverId() {
return driverId;
}
public void setDriverId(int driverId) {
this.driverId = driverId;
}
}
TtInvoice class
TtInvoice is created from a remote JSON object when the barcode on an invoice is scanned, have added a constructor just for testing purposes.
public class TtInvoice implements Serializable {
@JsonProperty("Invoice_Number")
private int invoiceNumber;
@JsonProperty("Invoice_Date")
private Date invoiceDate;
@JsonProperty("Invoice_Time")
private String invoiceTime;
@JsonProperty("Debtor_Ser_No")
private int debtorSerNo;
@JsonProperty("Debtor_Name")
private String debtorName;
@JsonProperty("COD_Customer")
private boolean codCustomer;
@JsonProperty("Outstanding_COD")
private boolean outstandingCOD;
@JsonProperty("Outstanding_COD_Amount")
private double outstandingCODAmount;
@JsonProperty("Delivery_Notes")
private String deliveryNotes;
@JsonProperty("Line_Count")
private int lineCount;
public TtInvoice(int invoiceNumber, Date invoiceDate, String invoiceTime, int debtorSerNo, int deliveryStatus) {
this.invoiceNumber = invoiceNumber;
this.invoiceDate = invoiceDate;
this.invoiceTime = invoiceTime;
this.debtorSerNo = debtorSerNo;
this.deliveryStatus = deliveryStatus;
}
public int getInvoiceNumber() {
return invoiceNumber;
}
public void setInvoiceNumber(int invoiceNumber) {
this.invoiceNumber = invoiceNumber;
}
public Date getInvoiceDate() {
return invoiceDate;
}
public void setInvoiceDate(Date invoiceDate) {
this.invoiceDate = invoiceDate;
}
public String getInvoiceTime() {
return invoiceTime;
}
public void setInvoiceTime(String invoiceTime) {
this.invoiceTime = invoiceTime;
}
public LocalDateTime getInvoiceDateTime() {
LocalTime time = LocalTime.parse(invoiceTime);
LocalDate date = invoiceDate.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
return date.atTime(time);
}
public int getDebtorSerNo() {
return debtorSerNo;
}
public void setDebtorSerNo(int debtorSerNo) {
this.debtorSerNo = debtorSerNo;
}
public String getDebtorName() {
return debtorName;
}
public void setDebtorName(String debtorName) {
this.debtorName = debtorName;
}
public String getDeliveryNotes() {
return deliveryNotes;
}
public void setDeliveryNotes(String deliveryNotes) {
this.deliveryNotes = deliveryNotes;
}
public boolean isCodCustomer() {
return codCustomer;
}
public void setCodCustomer(boolean codCustomer) {
this.codCustomer = codCustomer;
}
public boolean isOutstandingCOD() {
return outstandingCOD;
}
public void setOutstandingCOD(boolean outstandingCOD) {
this.outstandingCOD = outstandingCOD;
}
public double getOutstandingCODAmount() {
return outstandingCODAmount;
}
public void setOutstandingCODAmount(double outstandingCODAmount) {
this.outstandingCODAmount = outstandingCODAmount;
}
public int getLineCount() {
return lineCount;
}
public void setLineCount(int lineCount) {
this.lineCount = lineCount;
}
}
In my fragment, if I do
private RealmChangeListener<RealmResults<Customer>> customerRealmChangeListener;
RealmResults<Customer> customerRealmResults;
Realm realm = Realm.getDefaultInstance();
customerRealmChangeListener = customers -> {
customers.forEach(customer -> {
Log.w("CUSTOMERS", customer.getCustomerName());
});
};
customerRealmResults = realm.where(Customer.class).equalTo("invoices.deliveryStatus", 0).findAllAsync();
customerRealmResults.addChangeListener(customerRealmChangeListener);
realm.executeTransactionAsync(transactionRealm -> {
Customer newCustomer = new Customer(1, 1, "Test Customer 1");
Invoice newInvoice1 = new Invoice(new TtInvoice(1234, 20/02/2023, "10:07:00", 1, 0));
Invoice newInvoice2 = new Invoice(new TtInvoice(2468, 20/02/2023, "10:08:00", 1, 1));
newCustomer.getInvoices().add(newInvoice1);
newCustomer.getInvoices().add(newInvoice2);
transactionRealm.insert(newCustomer);
});
No issues with the adding, the problem I am having is the 'findAllAsync', it returns both invoices instead of only the 1.
I get the customer back as I would expect, but a call to 'customer.getInvoices()' returns 2 invoices instead of just 1.
I feel like what I am trying to do is super simple and I'm just not seeing the problem, have been trying to figure this out for a couple of days now to no avail.
If I set a breakpoint on the findAllAsync() call and run my own 'findAll()' using the same query, it has the same issue.
Maybe it is how I am adding new invoices, not 100% sure on that one though.
Otherwise I am liking Realm a hell of a lot better than Room.