How to update a Child object in Realm Android

Spaceghost87

I'm using realm java in android, i have a parent class and a child class, here is my code, the problem is that every time this code is called, a new child object is created instead of update the Parent, so if I call this five times, i have 1 Parent and 5 Child objects, i just need 1 Parent and 1 Child, there is a way to achieve this

 class Parent extends RealmObject {
    @PrimaryKey
    private String someKey;
     Child child;
 }

 class Child extends RealmObject {
    private int someValue;
 }

 final Parent parent = realm.where(parent.class).equalTo("somekey", key)
        .findFirst();


    realm.executeTransaction(new Realm.Transaction() {
      @Override
      public void execute(Realm realm) {
        Child child = realm.createObject(Child
            .class);
        child.setSomeValue(value);

        parent.setChild(child);

      }
    });
Christian Melchior

I'm not entirely sure why you are creating a Child object each time you update the Parent, but if the idea is that the parent should always have a child, then you can do this:

realm.executeTransaction(new Realm.Transaction() {
  @Override
  public void execute(Realm realm) {
    if (parent.getChild() == null) { // Only create a child if it is missing.
      Child child = realm.createObject(Child.class);
      parent.setChild(child);
    }
    parent.getChild().setSomeValue(value);
    parent.setNotificationConstants(notificationConstants);
  }
});

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how to update an object in realm swift

How to delete a Realm child object correctly?

Realm Java Android: Create or update object

How to close realm object in android

Realm.io - How to update object?

How to update the values in Table using Realm android?

Unable to update realm object

How to delete object from Realm Database Android?

Android: How to create empty realm object?

Realm: sorting by property in child object

Update statement in Realm android

Realm Update failed - Android

Realm.js - How to addListener to a single object and update UI

How can I add or update a Realm object in Swift?

Using realm how can i update data,It should not create new object only update object

how to set current timestamp for every insert and update row in realm android

How to update linearlayout child view programmatically in android?

Android Realm - Accessing Realm Object from Service

How to delete all records from realm object without fetching it in android?

Realm: How to add two Results<(Object)> in Realm

How can I delete a realm parent object along with all of its child objects?

Android Realm, query objects by child attribute

how to setState update property of object instance in child list

How to update a child of a firebase object using angularfire2

How to update child list with another object in Entity Framework Core

How to search string in nested object and update the property for all child and parent

Realm Swift : Update an object inside a closure

Realm object does not update, after adding objects in it

Update Additional Field on Realm Object with ObservedRealmObject in SwiftUI