我想通过获取所有记录来清除所有数据并一一删除:
- (void) clear{
for (Program *program in [self getAllProgram]){
[managedObjectContext deleteObject:program];
}
if (![managedObjectContext save:&error])
{
NSLog(@"Problem deleting program: %@", [error localizedDescription]);
}
}
- (NSArray *)getAllProgram{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Program"inManagedObjectContext:managedObjectContext];
[request setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat: @"freq = %@", self.freq];
[request setPredicate:predicate];
// Order the events by creation date, most recent first.
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"sid" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[request setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];
NSError *error = nil;
return [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
}
错误是
CoreData:错误:严重的应用程序错误。在核心数据更改处理期间捕获到异常。这通常是 NSManagedObjectContextObjectsDidChangeNotification 观察者中的一个错误。-[__NSCFSet removeAllObjects]:使用 userInfo (null) 将变异方法发送到不可变对象
谁能告诉我原因或告诉我一个好的做法?谢谢!