0

I am working on app in which i have to create backup of particular almirah of books & have to restore later.

There are multiple almirahs & each almirah have multiple books & each book have multiple chapters & so on. In core data i am using a single persistence store to save my data. entities have to-one, to-many & many to many kind of relationships.

One requirement in the app is to sync almirahs with server. User can update almirah, book & questions etc.

What i want is before syncing data to server i want to take backup of a particular almirah which i need to sync with server including all relationship etc. SO that i can restore that almirah later if required.

Restore is required because multiple users can sync same almirah & because of that after sync the data wouldn't be same as it was before sync.

I read this SO link but not get through much. Any help would be appreciated. How can I duplicate, or copy a Core Data Managed Object?

Shall i use multiple persistence stores?

Community
  • 1
  • 1
stackNeverFlow
  • 203
  • 4
  • 16

1 Answers1

0

Your requirement is better handled outside core data in as much as you're saving a checkpoint on the entire datastore, so you don't want to copy the items in the datastore, you want to copy the whole datastore as a single entity.

To do this you could store each 'copy' of the datastore in a different folder, so you'd have for example folders working, v1, v2 (you can decide on how to manage the versions). When you want to copy you would save the working version and then use NSFileManager to copy all of the files in the working folder to v3.

Note, your datastore is probably stored as multiple files so don't simply try to copy the .sqlite file on its own.

To restore an old version you would destroy all contexts and managed objects from the current store and then copy the version to be restored, replacing the current working folder.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • shouldn't there will be a simple way to handle this. i mean can't i just take backup of a single record from an entity & later restore that? Like in Sql we can just take backup of single record using sql queries & later we can update on the basis of backed up data? – stackNeverFlow Mar 22 '16 at 06:45
  • You could copy all the details of a single managed object if you want, using your own scheme - core data isn't designed specifically with that in mind – Wain Mar 22 '16 at 07:44