title: How to create snapshot of CoreData state?
body: <h3>Background story</h3>
  
  <p>I am developing a big iOS app. This app works under specific assumptions. The main of them is that app should work offline with internal storage which is a snapshot of last synchronized state of data saved on server. I decided to use CoreData to handle this storage. Every time app launches I check if WiFi connection is enabled and then try to synchronize storage with server. The synchronization can take about 3 minutes because of size of data.</p>
  
  <p>The synchronization process consists of several stages and in each of them I:</p>
  
  <ul>
  <li>fetch some data from the server (XML)</li>
  <li>deserialize it</li>
  <li>save it in Core Data</li>
  </ul>
  
  <h3>Problem</h3>
  
  <p>Synchronization process can be interrupted for several reasons (internet connection, server down, user leaving application, etc). This may cause data to be out-of-sync.</p>
  
  <p>Let's assume that synchronization process has 5 stages and it breaks after third. It results in 3/5 of data being updated in internal storage and the rest being out of sync. I can't allow it because data are strongly connected to each other (business logic).</p>
  
  <h3>Goal</h3>
  
  <p>I don't know if it is possible but I'm thinking about implementing one solution. On start of synchronization process I would like to create snapshot (some kind of copy) of current state of Core Date and during synchronization process work on it. When synchronization process completes with success then this snapshot could overwrite current CoreData state. When synchronization interrupts then snapshot can be simply aborted. My internal storage will be secured.</p>
  
  <h3>Questions</h3>
  
  <ul>
  <li>How to create CoreData snapshot?</li>
  <li>How to work with CoreData snapshot?</li>
  <li>How to overwrite CoreDate state with snapshot?</li>
  </ul>
  
  <p>Thanks in advice for any help. Code examples, if it is possible, will be appreciated.</p>
  
  <h3>EDIT 1</h3>
  
  <p>The size of data is too big to handle it with multiple CoreData's contexts. During synchronization I am saving current context multiple times to cleanup memory. If I do not do it, the application will crash with memory error.</p>
  
  <p>I think it should be resolved with multiple <code>NSPersistentStoreCoordinator</code>s using for example this method: <a href="https://developer.apple.com/documentation/coredata/nspersistentstorecoordinator/1468917-replacepersistentstore" rel="nofollow noreferrer">link</a>. Unfortunately, I don't know how to implement this.</p>