Mastering In CoreData (Part 17 Multithreading Concurrency Strategy Context UndoManager)
This part is the continuation of the previous part. It is mandatory to look the temporary problem that we solved using parent child strategy. Now in this part we are going to solve this problem using undo manager . To know what is the temporary change problem please refer to previous part
In part 2 we said
Managed Object Context (MOC)
Undo Manager
UndoManager provides a simple way to add undo/redo functionality to your managed Object Context. It’s an Instance property of the managed object context.
Download the starter project or delete the application if you follow the tutorials. First comment out these lines as shown in Figure 1.
Understand Undo Manager from Flow Diagram
As you can see in Figure 2 we performed number of tasks to understand how undo manager works
As you can see in Figure 3 we proved flow diagram theory into code
As you can see no User object was printed after undo operation . By doing redo two user object printed on the console
Note: First beginUndoGrouping increment grouping level to 2 .
Nested Undo Group
Performs the undo operations in the last undo group
As you can see in Figure 4. undoNestedGroup group undo last group which was when User 2 was added. As you can see we added User on first undo group and User 2 on second or last undo group. By performing undoNestedGroup it will undo last undo group which was where User2 was added. Now first undo group become the last after calling undoNestedGroup again it will delete User1 as well as shown in the Figure 4
As you can see in Figure 5 we proved it using code . Performing undo Nested group undo last group.
There are number of things you should know
undo → Closes the top-level undo group if necessary and invokes undoNestedGroup().
Solve Temporary Change Problem In Application Using Undo Manager
First delete the application and uncomment viewDidLoad and viewWillAppearmethod as shown in Figure 6. To understand the application, project structure and the problem we are going to solve referred to previous part then come again. In previous we solved the temporary changes problem using parent-child strategy , now we are going to solve it using context undoManger features.
As you can see in Figure 7 we did lot of things to setup undoManager
领英推荐
Now when user tap on back button we did a couple of things as shown in Figure 8
Run the application and run the flow which caused temporary changes was saving in database, now the problem is solved using undoManager.
RollBack Managed Object Context
Removes everything from the undo stack, discards all insertions and deletions, and restores updated objects to their last committed values
Understand Using Flow Diagram
As you can see in Figure 9 after calling rollback it discarded all temporary changes in the context and moved their state to last committed value which is the same state as of the persistent store. To illustrate this with example first delete the application and comment out viewDidLoad and viewwillAppear method on ViewController.swift
As you can see in Figure 10 we did number of things
Reset Managed Object Context
Returns the context to its base state
Understand Using Flow Diagram
As you can see in the Figure 11 it performs the same thing as the rollback did. We first saved User1 into the persistent store after that we cached User1in Managed Object Context . Then we temporarily added User2 and User3which was not in the persistent store yet. After that we called reset method on the context and it did the same thing as rollback did,but there was a slightly difference.It again fetched User1 from persistent store that’s why it’s address was changed whereas in rollback User1 address was the same.
As you can see in Figure 12 t we did number of things and delete the application
Note: All the receiver’s managed objects are “forgotten.” If you use this method, you should ensure that you also discard references to any managed objects fetched using the receiver, since they will be invalid afterwards.
Difference Between Reset and RollBack
Delete the application first
Reset : As you can see reference change and data became faulty also. Since it gets data from a persistent store as shown in Figure 13.
Delete the application first
Rollback : Reference is the same and since it didn’t delete cache data. The data still presents in the cache as shown in Figure 14