title: Realm: delete stale objects
body: <p>I often end up fetching arrays of objects from a backend and storing them into realm.</p>
  
  <p>When fetching the list from the backend server to update the realm db it can happen that some objects have been modified and some have been removed from the server:</p>
  
  <ul>
  <li><p>The modified objects are recognized by their primary key and automatically updated by realm when using copyToRealmorUpdate().</p></li>
  <li><p>The removed objects are instead left as-is in the realm db and I have no means to understand that there's now stale data in the realm.</p></li>
  </ul>
  
  <p>The first thing that came to  my mind is to delete all the objects of that type from realm before the update. But this will lead to a poor UX because the objects themselves are usually data bound to a scroll view in the app's UI (so the user will see the scroll view becoming empty and then full again).</p>
  
  <p>The second thing would be to check (using their primary key) which objects present in the realm DB are missing from the server's update list and remove them one by one.</p>
  
  <p>I'm feeling both of these approaches are suboptimal, is there a better way to handle this?</p>
  
  <p>Thanks,
  Bye,
  Marco</p>