title: Read objects in many-to-many relationship in Realm?
body: <p>I am performing many to many relationship in Realm and i am trying to read objects in Realm but i ain't getting Realm Results. This is how i'm storing objects in Realm.</p>
  
  <pre><code>fun addMessage(message : Message?,ref_id : String?){
      Realm.getDefaultInstance().use {realm -&gt;
          realm.executeTransaction {realm -&gt;
              val num = realm.where&lt;Messages&gt;().max("auto_index")
              var nextVal : Int ?= null
              if (num == null){
                  nextVal = 1
              }else{
                  nextVal = num.toInt() + 1
              }
              val msg = Messages()
              msg.messageId = message?.message_uid
              msg.body = message?.body
              msg.isCheck = message?.isCheck
              msg.auto_index = nextVal
  
              val messa = realm.where&lt;MessageRef&gt;().equalTo("messageRefId",ref_id).findFirst()
              if (messa != null){
                  messa.messages?.add(msg)
              }
              else {
                  val messageRef = MessageRef()
                  messageRef.messageRefId = ref_id
                  messageRef.messages = RealmList&lt;Messages&gt;()
                  messageRef.messages?.add(msg)
                realm.copyToRealmOrUpdate(messageRef)
              }
             realm.copyToRealmOrUpdate(msg)
          }
      }
  }
  </code></pre>
  
  <p>That's how i'm reading objects from database.</p>
  
  <pre><code>   fun getAllMessages (realm : Realm?,messageRefId : String?)=
      realm?.where&lt;MessageRef&gt;()?.equalTo("messageRefId",messageRefId)?.findFirst()
              ?.messages?.sort("auto_index")
  </code></pre>
  
  <p>}</p>
  
  <p>Below is my MessageRef model.</p>
  
  <pre><code>open class MessageRef : RealmObject() {
  @Required
  @PrimaryKey
  var messageRefId : String ?= null
  
  var messages : RealmList&lt;Messages&gt; ?= null
  }
  </code></pre>
  
  <p>Below is my Messages Model.</p>
  
  <pre><code>open class Messages : RealmObject(){
  @Required
  @PrimaryKey
  var messageId : String ?= null
  
  @Required
  var auto_index : Int ?= null
  
  @Required
  var body : String ?= null
  
  @Required
  var isCheck : String ?= null
  }
  </code></pre>
  
  <p>But I'm not getting any results. I debug the code and found that objects are successfully storing but not able to read.Below is my activity code.</p>
  
  <pre><code>override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.main_chat_page)
      initialize()
      message_ref_id = intent?.getStringExtra("conversation_id")
      toolbar_name?.text = name
      messageList = realmMessageController?.getAllMessages(realm,message_ref_id)
  }
  </code></pre>
  
  <p>Please tell me anybody why i ain't getting result.  </p>