realm-java: BUG: Inserted sub-objects not being linked to parent object

We have a simple scenario that we cannot seem to get working despite trying every approach we’ve seen in the docs/issue list.

We have two objects (Chat/Message), with chat having a list of messages: NOTE: We use Kotlin

@RealmClass
class Chat, RealmModel {
    ....
    var messages = RealmList<Message>()
    ....
}

@RealmClass
class Message : RealmModel {
    @PrimaryKey
    lateinit var id:String
    ....
}

When we insert new Messages into the Chat the Message object is being inserted in the database correctly but the Chat object is not linked to it – despite the Message being inserted THROUGH the Chat object (definitely a bug)

We’ve verified in the Realm Browser and there are indeed no linked messages (ie: it’s a persitence issue not a querying one)

Insertion code:

val chatService:ChatService = ....
val chatID:String = ...

// insertion code
val database:Realm = Realm.getDefaultInstance()
var message:Message = Message.fromJSON( dataJSON )
database.beginTransaction()
// message = database.copyToRealmOrUpdate( message ) // tried this too, makes no difference
val chat:Chat = chatService.getChatWithID( chatID )
// database.copyToRealmOrUpdate( chat ) // tried this too, makes no difference
chat.messages.add( message )
database.commitTransaction()

NOTE: We’ve also tried manually patching the object with JSON and we get the exact same effect (messages are updated, but chat does not have them linked)

val chatData:Map<String, Any?> = mapOf(
    "id" to chatID,
    "messages" to chat.messages.map{ ... conversion to JSON ... }
)
database.createOrUpdateObjectFromJson( Chat::class.java, JSONObject( chatData ))
database.commitTransaction()

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (11 by maintainers)

Most upvoted comments

@kneth please await us trying this out and confirming that it IS still an issue (from our code the object looks like it is indeed managed). We’re still awaiting our current dev cycle to finish but testing this out is in our next sprint. We can then move forward on FIXING the issue as opposed to closing it unresolved.