title: Android Room database transactions
body: <p>With the new Room Database in Android, I have a requirement where there are two sequential operations that needs to be made:</p>
  
  <pre><code>removeRows(ids);
  insertRows(ids);
  </code></pre>
  
  <p>If I run this, I see (on examining the db) that there are some rows missing - I assume they are being deleted after inserting. viz. the first operation is running in parallel to the second.</p>
  
  <p>If I use a transaction block, such as this, then it's all fine - the first operation seems to complete before doing the second:</p>
  
  <pre><code>roomDb.beginTransaction();
  removeRows(ids);
  roomDb.endTransaction();
  
  insertRows(ids);
  </code></pre>
  
  <p>It's also fine if I give a sleep in-between instead:</p>
  
  <pre><code>removeRows(ids);
  Thread.sleep(500);
  
  insertRows(ids);
  </code></pre>
  
  <p>There doesn't seem to be much documentation for Room, and was wondering if I should use the transaction block like the above when I have sequential operations to be done, or is there any better way of doing it.</p>
  
  <p><strong>EDIT</strong>: After @CommonsWare pointed out, <code>@Query</code> are asynchronous, while <code>@Insert</code> and <code>@Delete</code> are synchronous. In view of this, how would I get a query which deletes rows to be async:</p>
  
  <pre><code>@Query("DELETE from table WHERE id IN(:ids)")
  int removeRows(List&lt;Long&gt; ids);
  </code></pre>
  
  <p>According to the build output I get <code>Deletion methods must either return void or return int (the number of deleted rows)</code>, if I try to wrap the return type in a <code>Flowable</code>.</p>