这是我在 StackOverflow 上的第一个问题。如果我犯了一些错误,请原谅我。我正在尝试删除提交对象中的所有 NSManagedObject ESAnswer。
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);
}
但是控制台输出就像没有被删除(所有对象都在同一个 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
在我计算下一步之前,应该删除这些答案。但是在运行时,在删除之前使用旧数据调用计算方法。当我让它停留一段时间时,使用新数据调用计算方法。
所以,我的问题是: 1. 在上下文中删除 NSManagedObject 是否需要一些时间才能完成?2.如果是这样,我怎样才能让计算方法等待删除完成?
先感谢您!