title: RecyclerView and Adapter data updates
body: <p>This is a question about RecyclerView internal behavior for someone that knows its mechanics or is willing to dig into the source code. I’d like an answer backed up by references to the source.</p>
  <h1>Original question</h1>
  <p><em>(scroll down to ‘In other words’ for a more focused question)</em></p>
  <p>I need to understand how <code>notify*</code> actions (for example, <code>notifyItemInserted()</code>) are enqueued. Imagine I have an adapter backed up by this list:</p>
  <pre><code>ArrayList&lt;String&gt; list = Arrays.asList(&quot;one&quot;, &quot;three&quot;, &quot;four&quot;);
  </code></pre>
  <p>I want to add the values <code>zero</code> and <code>two</code>, that are missing.</p>
  <h3>Example 1</h3>
  <pre><code>list.add(1, &quot;two&quot;);
  // notify the view
  adapter.notifyItemInserted(1);
  
  
  // Seconds later, I go on with zero
  list.add(0, &quot;zero&quot;);
  // notify the view
  adapter.notifyItemInserted(0);
  </code></pre>
  <p>This is pretty straightforward and clear, nothing to tell.</p>
  <h3>Example 2</h3>
  <p>But what if the two actions are very close to each other, and there’s no layout pass in between?</p>
  <pre><code>list.add(1, &quot;two&quot;);
  list.add(0, &quot;zero”);
  </code></pre>
  <p><strong>What should I do now?</strong></p>
  <pre><code>adapter.notifyItemInserted(1);
  adapter.notifyItemInserted(0);
  </code></pre>
  <p>Or maybe</p>
  <pre><code>adapter.notifyItemInserted(2);
  adapter.notifyItemInserted(0);
  </code></pre>
  <p>? From the adapter perspective, the list immediately switched from <code>one, three, four</code> to <code>zero, one, two, three, four</code> so the second option seems more reasonable.</p>
  <h3>Example 3</h3>
  <pre><code>list.add(0, “zero”);
  adapter.notifyItemInserted(0);
  list.add(2, “two”);
  adapter.notifyItemInserted(...)
  </code></pre>
  <p>What about it now? <code>1</code> or <code>2</code> ? The list was updated immediately after, but I am sure there was no layout pass in between.</p>
  <h3>Question</h3>
  <p>You got the main issue, and I want to know how should I behave in these situations. The real case is that I have multiple asynchronous tasks ending up in an <code>insert()</code> method. I can enqueue their operations, but:</p>
  <ol>
  <li>I don’t want to do that if there’s already an internal queue, and there surely is</li>
  <li>I don’t know what happens if two actions happen without a layout pass in between, see Example 3.</li>
  </ol>
  <h1>In other words</h1>
  <p>To update recycler, 4 actions must happen:</p>
  <ol>
  <li>I actually alter the data model (e.g. insert something into the backing array)</li>
  <li>I call <code>adapter.notify*()</code></li>
  <li>Recycler receives the call</li>
  <li>Recycler <strong>performs</strong> the action (e.g. calls <code>getItem*()</code> and <code>onBind()</code> on the adapter) and lays out the change.</li>
  </ol>
  <p>It’s easy to understand this when there’s no concurrency, and they happen in sequence:</p>
  <pre><code>1. =&gt; 2. =&gt; 3. =&gt; 4. =&gt; (new update) 1. =&gt; 2. =&gt; 3. =&gt; 4. ...
  </code></pre>
  <p>Let’s see what happens between steps.</p>
  <ul>
  <li>Between <strong>1.</strong> and <strong>2.</strong>: I would say it is the developer responsibility to call notify() immediately after having altered the data. That’s OK.</li>
  <li>Between <strong>2.</strong> and <strong>3.</strong>: This happens immediately, no issue here.</li>
  <li>Between <strong>3.</strong> and <strong>4.</strong>: This does <strong>not</strong> happen immediately! AFAIK. So it perfectly possible that a new update (steps <strong>1</strong> and <strong>2</strong>) comes between steps <strong>3</strong> and <strong>4</strong> <strong>of the previous update</strong>.</li>
  </ul>
  <p>I want to understand what happens in this case.
  How should we behave?
  Should I ensure that step <strong>4</strong> of the previous update did took place before inserting new stuff? If so how?</p>