Core Data and Magical Record saving
-
24-12-2019 - |
Question
This is somewhat of a basic question, but need some clarification.
I have this piece of code.
//line of code #1
[localContext MR_saveToPersistentStoreWithCompletion:^(BOOL success, NSError *error) {
//line of code #2
}];
Is the line of code #2 the completion block? Should I make my changes to an entity prior to the save as line of code #1 shows?
Thanks
Solution
Anything with the form of ^(…) { … }
or ^{ … }
is a block. In essence, a block is portable a chunk of code which is not directly executed. A block can be saved to a variable then execute at a later point in time.
typedef void (^my_block_t)();
my_block_t myBlock = ^{
NSLog(@"I'm in a block");
};
Here's a block which prints a log statement. The block is saved to the variable myBlock
. Note: at this point in time, NSLog
does not print out anything.
At some point in time later, myBlock
can be execute.
myBlock();
At this point in the code, the block is run and NSLog
prints out it's statement.
If your example, the execution of line of code #2 is delayed until after the save has completed.
Logically, it works the same as:
Step 1) execute line of code #1
Step 2) execute save
Step 3) execute line of code #2
Because things in line of code #2 happen after the save has completed, then any changes to the model would not be saved at that point.