Devarshi
Devarshi

Reputation: 16758

Core data : how to undo operations once managed objects are saved with context

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.

Upvotes: 0

Views: 376

Answers (1)

Tom Harrington
Tom Harrington

Reputation: 70956

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.

Upvotes: 1

Related Questions