Leosan Leosan - 1 month ago 7
Android Question

Realm object not fully persisted

I'm having trouble trying to understand how realm.io persist/save objects.

I have 3 Objects (Inventory, InventoryItem and Product);
When I create a Inventory containing InventoryItems it works fine until i close the app. When i re-open the app all InventoryItems loses the reference to Product and start to show "null" instead.

Strange thing is all other attributes like Inventory reference to InventoryItem is persisted fine. Just problem with Products.

this is how i'm trying to do:

Model
Product

public class Product extends RealmObject {
@PrimaryKey
private String id;
@Required
private String description;

private int qtdUnityType1;
private int qtdUnityType2;
private int qtdUnityType3;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public int getQtdUnityType1() {
return qtdUnityType1;
}

public void setQtdUnityType1(int qtdUnityType1) {
this.qtdUnityType1 = qtdUnityType1;
}

public int getQtdUnityType2() {
return qtdUnityType2;
}

public void setQtdUnityType2(int qtdUnityType2) {
this.qtdUnityType2 = qtdUnityType2;
}

public int getQtdUnityType3() {
return qtdUnityType3;
}

public void setQtdUnityType3(int qtdUnityType3) {
this.qtdUnityType3 = qtdUnityType3;
}


}

Inventory

public class Inventory extends RealmObject {

@PrimaryKey
private String id;

@Required
private String type;

@Required
private Date createdAt;

@Required
private String status;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

private RealmList<InventoryItem> listItems;


public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public Date getCreatedAt() {
return createdAt;
}

public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}

public RealmList<InventoryItem> getListItems() {
return listItems;
}

public void setListItems(RealmList<InventoryItem> listItems) {
this.listItems = listItems;
}


}
InventoryItem

public class InventoryItem extends RealmObject {

@PrimaryKey
private String idItem;

private Inventory inventory;

private Product product;

public Product getProduct() {
return product;
}

public void setProduct(Product product) {
this.product = product;
}


private Date expirationDate;

private int qtdUnityType1;

private int qtdUnityType2;

private int qtdUnityType3;

private int qtdDiscard;

public String getIdItem() {
return idItem;
}

public void setIdItem(String idItem) {
this.idItem = idItem;
}

public Inventory getInventory() {
return inventory;
}

public void setInventory(Inventory inventory) {
this.inventory = inventory;
}


public Date getExpirationDate() {
return expirationDate;
}

public void setExpirationDate(Date expirationDate) {
this.expirationDate = expirationDate;
}

public int getQtdUnityType1() {
return qtdUnityType1;
}

public void setQtdUnityType1(int qtdUnityType1) {
this.qtdUnityType1 = qtdUnityType1;
}

public int getQtdUnityType2() {
return qtdUnityType2;
}

public void setQtdUnityType2(int qtdUnityType2) {
this.qtdUnityType2 = qtdUnityType2;
}

public int getQtdUnityType3() {
return qtdUnityType3;
}

public void setQtdUnityType3(int qtdUnityType3) {
this.qtdUnityType3 = qtdUnityType3;
}

public int getQtdDiscard() {
return qtdDiscard;
}

public void setQtdDiscard(int qtdDiscard) {
this.qtdDiscard = qtdDiscard;
}


}

and finally one of the millions ways i tried to persist

realm.beginTransaction();

Inventory inventory = realm.createObject(Inventory.class);
inventory.setId(id);
inventory.setCreatedAt(new DateTime().toDate());
if (radioGroup.getCheckedRadioButtonId() == R.id.rbInventario) {
inventory.setType("Inventário");
} else {
inventory.setType("Validade");
}
inventory.setStatus("Aberto");

RealmList<InventoryItem> inventoryItems = new RealmList<>();
RealmResults<Product> productsRealmResults = realm.allObjects(Product.class);


for (int i = 1; i <= productsRealmResults.size(); i++) {
InventoryItem item = realm.createObject(InventoryItem.class);
item.setIdProduct(productsRealmResults.get(i - 1).getId() + " - " + productsRealmResults.get(i - 1).getDescription());
item.setProduct(productsRealmResults.get(i - 1));
item.setIdItem(i + "-" + id);
item.setInventory(inventory);
item = realm.copyToRealmOrUpdate(item);
item = realm.copyToRealmOrUpdate(item);
inventoryItems.add(item);
}
inventory.setListItems(inventoryItems);


realm.copyToRealmOrUpdate(inventory);
realm.commitTransaction();


I already looked trough some answers here like this one:
stack answer
and the Java-examples (person, dog, cat)

provided with the API

but I can't understand how to properly insert this.

Answer

The problem is that you are setting a list of InventoryItem elements which are not added to the Realm database.

Change InventoryItem item = new InventoryItem(); to InventoryItem item = realm.createObject(InventoryItem.class);

Also, the inventoryItems themselves aren't stored in Realm db. Add realm.copyToRealmOrUpdate(inventoryItems) after the loop.