title: Snapshots of data in iCloud enabled Core Data app?
body: <p>My app syncs and backups its data via iCloud (I'm using <a href="https://github.com/mluisbrown/iCloudCoreDataStack" rel="nofollow">iCloudCoreDataStack</a> for this purpose). As my app handles important data created by the user, I want to offer an additional safety net by the ability to create snapshots from time to time which can be restored later. </p>
  
  <p>I'm not talking about undoing one or two single changes, but about real snapshots, timestamped and stored in a save manner.</p>
  
  <p>As I am leveraging the iCloud sync, I cannot simply exchange the sqlite store used in the app. I think I have to delete every object in the current context, create the objects restored from the snapshot and save the context. Then, the device will push the changes to iCloud too.</p>
  
  <p>I'm thinking of the following scenario:</p>
  
  <ol>
  <li>The app works normal without the option to create snapshots (it's just an optional safety feature for the user).</li>
  <li>If the user opts in to create snapshots from time to time, he's asked to enter his Dropbox login credentials (already implemented).</li>
  <li>From time to time, my app then creates a background thread in which a second store and a second MOC (and a second PSC?) will be set up. Every object together with its relationships gets copied into this 2nd MOC. The 2nd MOC gets saved, the persistent store will be copied to Dropbox and the thread will end.</li>
  <li>If the user wants to restore a snapshot and discard every change made after the creation of the snapshot, my app will rebuild the data by reversely doing what's described in point 3.</li>
  </ol>
  
  <p>Any thoughts of that? Is this save to do, are there any caveats to think of (esp. together with iCloud)? Has anyone done this yet in a similar way?</p>
  
  <hr>
  
  <p><strong>Edit:</strong></p>
  
  <p>I was successful in creating a copy of the store with the steps described above. Recreation of the store seemed also to work in some first tries. But, I still have worries about my way of loading the store file (pulling the old file away and then replacing it with my saved one).</p>
  
  <p>However, I've come across Apples "<em>Save as...</em>" or "<em>Relocate</em>" method </p>
  
  <pre><code>NSDictionary *options = @{
      NSPersistentStoreRemoveUbiquitousMetadataOption: @YES
  };
  NSPersistentStore *copy = [psc migratePersistentStore:currentiCloudStore
                                                  toURL:url
                                                options:options
                                               withType:NSSQLiteStoreType
                                                  error:&amp;error];
  </code></pre>
  
  <p>which will remove all iCloud associated metadata, and also removes the current store from the coordinator (I don't want that) and adds the newly created store to the PSC (I don't want that either).</p>
  
  <p>What is the better way to go? My current approach or Apples's built in "<em>Save as... / Relocate</em>"? My focus lies on loading the snapshots back in later, no matter if iCloud was/is enabled or not.</p>