2

I am trying to learn Realm basics by implementing a simple Android project. The idea is that user have several items and several item lists and an item can be added to any of these lists and a list can have many items. Therefore, there is a many to many relationship between Item and List objects. Here are my objects.

public class Item extends RealmObject {

    @PrimaryKey
    private String id;
    private String name;
    private boolean isDone;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isDone() {
        return isDone;
    }

    public void setDone(boolean done) {
        isCollected = done;
    }
}

public class List extends RealmObject {

    @PrimaryKey
    private String id;
    private String name;
    private RealmList<Item> items;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public RealmList<Item> getItems() {
        return items;
    }

    public void setItems(RealmList<Item> items) {
        this.items = items;
    }
}

My problem is, the field isDone might be different depending on the item's status in a given list. But when I update this field in a list, all the other items added to different lists get updated too. Since they are using the same object it makes sense but that is not to behavior I want. Is there a Realm way to create a pivot table/object with an extra column/field (in that case isDone) ?

Any help would be appreciated.

Tartar
  • 5,149
  • 16
  • 63
  • 104
  • `But when I update this field in a list, all the other items added to different lists get updated too.` what do you mean? show code – EpicPandaForce Jul 15 '18 at 18:36
  • Let say there is one Item object and I am using it in different Lists. When I change this object's isDone field, it changes in other lists too. – Tartar Jul 16 '18 at 05:44

1 Answers1

0

The problem is that the isDone property of Item doesn't truly belong to Item. If you can set the same Item in multiple Lists, then the property that a given task is deemed complete within a given List is the property of the List.

public class Item extends RealmObject {

    @PrimaryKey
    private String id;
    private String name;
    //private boolean isDone;

    @LinkingObjects("completedItems")
    private final RealmResults<List> tasksCompletedIn = null;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    //public boolean isDone() {
    //    return isDone;
    //}

    //public void setDone(boolean done) {
    //    isDone = done;
    //}
}

public class List extends RealmObject {

    @PrimaryKey
    private String id;
    private String name;
    private RealmList<Item> items;
    private RealmList<Item> completedItems;

Then you know if it's a completed item if completedItems.contains(item) (where item is a managed object, or overrides equals to check against only id)

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428