0

I am trying to implement downloading of bulk data from several tables on the server.

In my case there are 16 tables. For all these tables I will be firing 10 requests to the server. This means I have done a bit of logical groupings for related tables, but it is like all tables are inter-related with each other through one or the other relationship.

I need to consider three cases while doing downloading:

  1. Saving data to each table at local.
  2. Managing relationships between inserted objects.
  3. Handling situation when one of the requests fails during download, say 8th request failed.

I will be following this approach for each response:

  1. Inserting data in managed object context.
  2. Managing relationships by firing NSPredicate and associating the related objects.
  3. Saving the context.

In case of a response failure, I have two options:

  1. Next time continue from the failed response.
  2. Revert all saved data to its previous state.

1st approach may lead to some data inconsistency, so I am going with 2nd approach.

I know that if a managed object context is not saved, we can revert the changes, but

is it possible to revert the changes, if the managed object context is saved?

I require some useful answers from the community.

Please suggest.

Devarshi
  • 16,440
  • 13
  • 72
  • 125

1 Answers1

1

Is it possible to revert the changes, if the managed object context is saved?

After saving? Maybe, but it could be tricky. If you set up a separate managed object context for your network operations, and give it an NSUndoManager, you could later on tell the undo manager to roll everything back to the previous state.

It would be simpler to just not save changes until you're finished, though. Using an undo manager doesn't really help much-- the memory needed to store up all the undo actions will at least match the memory use from keeping all of the unsaved changes around until you're finished. If you're working on a separate managed object context (whether a child context or a completely separate context), handling the error case is as simple as letting the MOC get deallocated without saving changes first.

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
  • Hi Tom, thanx for your reply :-).. I have one more query: suppose if I decide to save everything say at the end of 10th (in my case last) response, wouldn't managed object context be consuming a lot of memory, considering the amount of data? – Devarshi Feb 08 '13 at 04:35
  • Potentially yes, depending on how big the import is. But there's no good way to roll back changes *after saving them*. There's no checkpoint mechanism, for example. If you really need all 10 imports to work for data integrity you may have a problem. Maybe you can test them before beginning the import process? You might also find this guide from Apple to be useful: https://developer.apple.com/library/mac/ipad/#documentation/Cocoa/Conceptual/CoreData/Articles/cdImporting.html – Tom Harrington Feb 08 '13 at 04:47
  • once again thnx for your useful input :-) – Devarshi Feb 08 '13 at 04:50