This is my first question on StackOverflow. Pardon me if I make some mistakes. I'm trying to delete all NSManagedObject ESAnswer in a Submission object.
NSLog(@"BEFORE");
for (ESAnswer *ans in _currentSubmission.answer) {
NSLog(@"%@", ans.questionID);
}
// Delete all answer with index from _nextQuestionsIndexStack.m_array
for (NSNumber *index in _nextQuestionsIndexStack.m_array) {
// Find ESAnswer object with questionID
NSString *questionID = _question[index];
NSPredicate *predicateAnswer = [NSPredicate predicateWithFormat:@"questionID LIKE %@", questionID];
ESAnswer *foundAnswer = [[_currentSubmission.answer filteredSetUsingPredicate:predicateAnswer] anyObject];
// Delete object
[context deleteObject:foundAnswer];
NSLog(@"Deleted %@", foundAnswer.questionID);
}
NSLog(@"AFTER");
for (ESAnswer *ans in _currentSubmission.answer) {
NSLog(@"%@", ans.questionID);
}
But the console output was like nothing was deleted (all objects are in the same NSManagedObjectContext):
2013-09-18 11:01:36.541[3661:707] BEFORE
2013-09-18 11:01:36.543[3661:707] 52368675231dbba60e000065
2013-09-18 11:01:36.544[3661:707] 522ff38a93ebc0ce4f000008
2013-09-18 11:01:36.545[3661:707] 52368662231dbba60e000060
2013-09-18 11:01:36.553[3661:707] Deleted 52368675231dbba60e000065
2013-09-18 11:01:36.558[3661:707] Deleted 52368662231dbba60e000060
2013-09-18 11:01:36.563[3661:707] Deleted 522ff38a93ebc0ce4f000008
2013-09-18 11:01:36.564[3661:707] AFTER
2013-09-18 11:01:36.565[3661:707] 52368675231dbba60e000065
2013-09-18 11:01:36.567[3661:707] 522ff38a93ebc0ce4f000008
2013-09-18 11:01:36.570[3661:707] 52368662231dbba60e000060
These answers should be deleted before I calculate the next step. However in runtime, the calculate method is called with the old data before deleting. When I let it stay for a while, the calculate method is called with the new data.
So, my question is: 1. Is that deleting NSManagedObject in a context take some time to complete? 2. If that so, how can I make the calculate method wait for deleting to complete?
Thank you in advance!
deleteObject:
only marks the object for deletion. You can save
the context, or call
processPendingChanges
to make the context actually perform the changes to the object graph.
processPendingChanges
is called automatically during the main event loop, which explains
why you are seeing the changes "later".