### Background story

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.

The synchronization process consists of several stages and in each of them I:

* fetch some data from the server (XML)
* deserialize it
* save it in Core Data

### Problem

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.

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).

### Goal

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.

### Questions

* How to create CoreData snapshot?
* How to work with CoreData snapshot?
* How to overwrite CoreDate state with snapshot?

Thanks in advice for any help. Code examples, if it is possible, will be appreciated.

### EDIT 1

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.

I think it should be resolved with multiple `NSPersistentStoreCoordinator`s using for example this method: [link](https://developer.apple.com/documentation/coredata/nspersistentstorecoordinator/1468917-replacepersistentstore). Unfortunately, I don't know how to implement this.