Search code examples
swiftcore-datansfetchedresultscontroller

How to assign fetchedObjects to a variable independently


In my case i 'm getting my nsmanagedobjects into an array with line of

let rows = self.fetchedResultsController.fetchedObjects as [Row]

after this line i rollback changes as my needs

managedObjectContext?.rollback()

and i am supposed to use the values of the rows variable. But due to rollback() function changes in rows are also deleted.

How can i keep my data for after rollback() function call?


Solution

  • "But due to rollback() function changes in rows are also deleted."

    That's because this is specifically what that method is documented to do. The documentation says:

    Removes everything from the undo stack, discards all insertions and deletions, and restores updated objects to their last committed values.

    The entire purpose of calling rollback is to get rid of any unsaved changes in the context, so what you describe is the expected behavior.

    If you want to keep changes and call rollback for some reason, you would have to copy those changes to someplace not affected by the managed object context, then I guess copy them back later on. That will be a pain in the ass, but it's possible.

    The real question is why you are calling rollback-- a method specifically designed to undo unsaved changes-- if you have changes you don't want to undo.