Search in sources :

Example 1 with Object4957

use of io.realm.entities.Object4957 in project realm-java by realm.

the class RealmTests method copyToRealmOrUpdate_bug4957.

// Test to reproduce issue https://github.com/realm/realm-java/issues/4957
@Test
public void copyToRealmOrUpdate_bug4957() {
    Object4957 listElement = new Object4957();
    listElement.setId(1);
    Object4957 parent = new Object4957();
    parent.setId(0);
    parent.getChildList().add(listElement);
    // parentCopy has same fields as the parent does. But they are not the same object.
    Object4957 parentCopy = new Object4957();
    parentCopy.setId(0);
    parentCopy.getChildList().add(listElement);
    parent.setChild(parentCopy);
    parentCopy.setChild(parentCopy);
    realm.beginTransaction();
    Object4957 managedParent = realm.copyToRealmOrUpdate(parent);
    realm.commitTransaction();
    // The original bug fails here. It resulted the listElement has been added to the list twice.
    // Because of the parent and parentCopy are not the same object, proxy will miss the cache to know the object
    // has been created before. But it does know they share the same PK value.
    assertEquals(1, managedParent.getChildList().size());
    // insertOrUpdate doesn't have the problem!
    realm.beginTransaction();
    realm.deleteAll();
    realm.insertOrUpdate(parent);
    realm.commitTransaction();
    managedParent = realm.where(Object4957.class).findFirst();
    assertEquals(1, managedParent.getChildList().size());
}
Also used : Object4957(io.realm.entities.Object4957) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest)

Aggregations

UiThreadTest (androidx.test.annotation.UiThreadTest)1 Object4957 (io.realm.entities.Object4957)1 Test (org.junit.Test)1