Search code examples
androidrealm

How can I make a RealmProxy[Object] and its corresponding [Object] equal?


Ok, so I implemented Realm for my android App. The app is basically an accounting-shopping list hybrid where a user adds items he or she already purchased.

In my Accounting class, which holds the Realm database implementation, the method for adding an item returns an int to my main Activity to be used in RecyclerAdapter.notifyItemInserted.

Here's how the method is supposed to work

public int addItem(final Item latestItem) {
    mRealm.beginTransaction();
    Number firstId = mRealm.where(Item.class).max("ID");
    int nextId = firstId != null ? firstId.intValue() + 1 : 0;
    Item firstItem = mRealm.where(Item.class)
            .equalTo("mName", latestItem.getName())
            .equalTo("mPrice", latestItem.getPrice())
            .findFirst();

    if (firstItem == null) {
        latestItem.setID(nextId);
        mRealm.copyToRealm(latestItem);
    } else  {
        firstItem.setCount(firstItem.getCount() +  latestItem.getCount());
    }
    mRealm.commitTransaction();
    updateItemList();

    int index = mItemList.indexOf(latestItem);
    return index;

The problem is the the index is -1, i.e the object doesn't exist in the List object I populate with the database objects as presented here:

private void updateItemList() {
    mItemList = mRealm.where(Item.class).findAllSorted("mPrice", Sort.DESCENDING);
}

I've tried manually retrieving the object from the database and it's shown as a RealmProxyItem. Testing for equals() returns false. Casting just throws ClassCastException.

What do I do to ensure that the equals() returns true?

EDIT: Here's my Realm model

Item.java

public class Item extends RealmObject{
@PrimaryKey
private int ID;

private String mName;
private double mPrice;
private int mCount;
private int mItemType;

public Item() {/** Required due to Realm **/ }

public Item(String mName, double mPrice, int count, int itemType) {
    this.mName = mName;
    this.mPrice = mPrice;
    this.mCount = count;
    this.mItemType = itemType;
    this.ID = 0;
}

 /**public Item(String mName, double mPrice, int count) {
    this(mName, mPrice, count, ItemType.NO_TYPE);
} **/

public String getName() {
    return mName;
}

public void setName(String name) {
    mName = name;
}

public double getPrice() {
    return mPrice;
}

public int getCount() {
    return mCount;
}

public void setCount(int count) {
    mCount = count;
}

public int getItemType() {
    return mItemType;
}

public int getID() {
    return ID;
}

public void setID(int ID) {
    this.ID = ID;
}

/** public ItemType getItemType() {
    return mItemType;
}  **/

@Override
public boolean equals(Object object) {
    if (object == null) return false;
    else if (object.getClass() != this.getClass()) return false;

    Item item = (Item) object;

    if (!item.getName().equals(this.getName())) return false;
    else if (item.getPrice() != this.getPrice()) return false;
    //else if (!item.getItemType().equals(this.getItemType())) return false;

    return true;
}

@Override
public int hashCode() {
    int hash = 5;
    hash = 17 * hash + this.mName.hashCode();
    hash = 17 * hash + (int) (Double.doubleToLongBits(this.mPrice) ^ (Double.doubleToLongBits(this.mPrice) >>> 32));
    return hash;
}

public void increaseCountBy(int amount) {
    mCount += amount;
}
}   

Solution

  • Since you have a primary key in your model i'd say change your equals() method to this :

    @Override
    public boolean equals(Object object) {
        if (object == null || !(object instanceof Item)) return false;
        Item item = (Item) object;
    
        return item.getID() == ID;
    }