title: Realm OrderedRealmCollectionChangeListener does not work
body: <p>My current realm version : 4.3.3 , realm-adapter version : 2.1.1 (latest versions)</p>
  
  <p>I used OrderedRealmCollectionChangeListener as the Realm documentation : <a href="https://realm.io/docs/java/latest/#notifications" rel="nofollow noreferrer">https://realm.io/docs/java/latest/#notifications</a></p>
  
  <p>But OrderedRealmCollectionChangeListener is not triggered to when only first element is added to adapter until adapter is refreshing. I couldn't find where I made the mistake.</p>
  
  <p>Please help me.</p>
  
  <p>MyAdapter:</p>
  
  <pre><code> public class MyAdapter&lt;V extends View &amp; BindableItem&gt;
          extends MyRealmRecyclerViewAdapter&lt;Model, RecyclerView.ViewHolder&gt; {
  
      private final
      @NonNull
      LayoutInflater inflater;
  
      public MyAdapter(@Nullable ItemClickListener clickListener,
                                 OrderedRealmCollection&lt;Model&gt; data) {
  
          super(data);
  
          this.inflater = LayoutInflater.from(context);
          this.realm = MyApplication.getInstance().openRealm(this.getClass().getName());
  
      }
  
      public boolean realmControl() {
  
          if (realm == null || realm.isClosed()) {
  
              realm = MyApplication.getInstance().openRealm(this.getClass().getName());
  
              updateData(new ModelQueries().getAllModels(getRealm()));
              notifyView();
  
              return true;
          } else {
              return false;
          }
      }
  </code></pre>
  
  <p>MyRealmRecyclerViewAdapter:</p>
  
  <pre><code>abstract class MyRealmRecyclerViewAdapter&lt;T extends Model, VH extends RecyclerView.ViewHolder&gt; extends RealmRecyclerViewAdapter&lt;Model, VH&gt; {
  
      protected Context context;
      private String TAG = MyRealmRecyclerViewAdapter.class.getSimpleName();
  
      protected Realm realm;
  
      private OrderedRealmCollection&lt;T&gt; allModels;
      private int allModelsCount = 0;
      private final OrderedRealmCollectionChangeListener listener;
  
      MyRealmRecyclerViewAdapter(@Nullable OrderedRealmCollection&lt;T&gt; data) {
  
          super((OrderedRealmCollection&lt;Model&gt;) data, true, true);
  
          if (data != null &amp;&amp; !data.isManaged())
              throw new IllegalStateException("Only use this adapter with managed RealmCollection, " +
                      "for un-managed lists you can just use the BaseRecyclerViewAdapter");
  
          this.context = MyApplication.getContext();
          this.allModels = data;
          this.listener = createListener();
      }
  
      private OrderedRealmCollectionChangeListener createListener() {
          return new OrderedRealmCollectionChangeListener() {
              @Override
              public void onChange(@NonNull Object collection, OrderedCollectionChangeSet changeSet) {
  
                  Mylog.i(TAG, " changeSet : " + changeSet);
  
                  if (changeSet == null) {
                      return;
                  }
                  OrderedCollectionChangeSet.Range[] insertions = changeSet.getInsertionRanges();
  
                  if (insertions.length &gt; 0) {
                      newModelsControl(getItemCount() - allModelsCount, getItemCount() - 1);
                      refreshAllModelsCount();
                  }
              }
          };
      }
  
      @Override
      public int getItemCount() {
  
          try {
              if (getData() == null) {
                  return 0;
              } else {
                  return getData().size();
              }
          } catch (Exception e) {
              restartActivity();
              return 0;
          }
  
      }
  
      public void refreshAllModelsCount() {
          allModelsCount = getItemCount();
      }
  
      public Realm getRealm() {
  
          if (realm == null || realm.isClosed()) {
              realm = MyApplication.getInstance().openRealm(this.getClass().getName());
          }
  
          return realm;
      }
  
  
      private boolean isDataValid() {
          return getData() != null &amp;&amp; getData().size() &gt; 0 &amp;&amp; getData().isValid();
      }
  
      @Override
      public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
          super.onAttachedToRecyclerView(recyclerView);
          if (isDataValid()) {
              //noinspection ConstantConditions
              addListener(allModels);
          }
      }
  
      @Override
      public void onDetachedFromRecyclerView(final RecyclerView recyclerView) {
          super.onDetachedFromRecyclerView(recyclerView);
          if (isDataValid()) {
              //noinspection ConstantConditions
              removeListener(allModels);
          }
      }
  
      private void addListener(@NonNull OrderedRealmCollection&lt;T&gt; data) {
          if (data instanceof RealmResults) {
              RealmResults&lt;T&gt; results = (RealmResults&lt;T&gt;) data;
              //noinspection unchecked
              results.addChangeListener(listener);
          } else if (data instanceof RealmList) {
              RealmList&lt;T&gt; list = (RealmList&lt;T&gt;) data;
              //noinspection unchecked
              list.addChangeListener(listener);
          } else {
              throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass());
          }
      }
  
      private void removeListener(@NonNull OrderedRealmCollection&lt;T&gt; data) {
          if (data instanceof RealmResults) {
              RealmResults&lt;T&gt; results = (RealmResults&lt;T&gt;) data;
              //noinspection unchecked
              results.removeChangeListener(listener);
          } else if (data instanceof RealmList) {
              RealmList&lt;T&gt; list = (RealmList&lt;T&gt;) data;
              //noinspection unchecked
              list.removeChangeListener(listener);
          } else {
              throw new IllegalArgumentException("RealmCollection not supported: " + data.getClass());
          }
      }
  
  
  
      /**
       * Returns the item associated with the specified position.
       * Can return {@code null} if provided Realm instance by {@link OrderedRealmCollection} is closed.
       *
       * @param index index of the item.
       * @return the item at the specified position, {@code null} if adapter data is not valid.
       */
      @SuppressWarnings("WeakerAccess")
      public T getItem(int index) {
          try {
  
              return isDataValid() ? allModels.get(index) : null;
  
          } catch (Exception e) {
  
              Mylog.printStackTrace(" MyRealmRecyclerViewAdapter getItem error ", e);
              return null;
          }
      }
  }
  </code></pre>