title: Optimal way of syncing Core Data with server-side data?
body: <p>I have what I would presume is a very common situation, but as I'm new to iOS programming, I'm not sure of the optimal way to code it.</p>
  
  <p><strong>Synopsis:</strong></p>
  
  <p>I have data on a server which can be retrieved by the iPhone app via a REST service. On the server side, the data is objects with a foreign key (an integer id number).</p>
  
  <p>I'm storing the data retrieved via REST in Core Data. The managed objects have an "objId" attribute so that I can uniquely identify the managed objects in the rest of my code.</p>
  
  <p>My app must always reflect the server data.</p>
  
  <p>On subsequent requests made to the server: </p>
  
  <ul>
  <li><p>some objects may not be returned, they have been deleted on the server - in which case I need to delete the corresponding objects from Core Data - so that I'm reflecting the state of the server correctly.</p></li>
  <li><p>some objects have attributes which have changed, therefore the corresponding managed objects need updating with the new data.</p></li>
  </ul>
  
  <p><strong>my solution - and question to you</strong></p>
  
  <p>To get things going in my app, I made the easiest solution of deleting all objects in Core Data, then adding all new objects in, created with the latest server side data.</p>
  
  <p>I don't think this is the best way to approach it :) As I progress on with my app, I now want to link up my tableview with NSFetchedResultsController, and have realised that my approach of deleting everything and re-adding is not going to work any more.</p>
  
  <p>What is the tried and trusted way of syncing Core Data with server side data?</p>
  
  <p>Do I need to make a fetch request for each object id I get back from the server, and then update the object with the new data?</p>
  
  <p>And then go through all of the objects in core data and see which ones have not been updated, and delete those? </p>
  
  <p>Is that the best way to do it? It just seems a little expensive to do a fetch for each object in Core Data, that's all.</p>
  
  <p>Pseudo code is fine for any answers :)</p>
  
  <p>thanks in advance!</p>