我想从 CoreData 表中删除选定的项目列表:A number of Persons with certain namesToDelete
:
NSError* error = nil;
NSFetchRequest* request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Person"
inManagedObjectContext:managedObjectContext]];
NSPredicate* predicate = [NSPredicate predicateWithFormat:@"NOT (name IN %@)", namesToDelete];
[request setPredicate:predicate];
NSArray* deleteArray = [managedObjectContext executeFetchRequest:request error:&error];
if (error == nil)
{
for (NSManagedObject* object in deleteArray)
{
[managedObjectContext deleteObject:object];
}
[managedObjectContext save:&error];
//### Error handling.
}
else
{
//### Error handling.
}
这可行,但这是在 CoreData 中执行此操作的最简单/最短的方法吗?