I have a block predicate that I carefully crafted only to discover you can't use them in Core Data.
NSPredicate *rootContactPredicate = [NSPredicate predicateWithBlock:^BOOL(id evaluatedObject, NSDictionary *bindings) {
BOOL isPersonAndRoot = ([[[evaluatedObject entity] name] isEqualToString:@"Person"] && [[(Person*)evaluatedObject accounts] count] == 0);
BOOL isAccountAndRoot = ([[[evaluatedObject entity] name] isEqualToString:@"Account"] && [(Account*)evaluatedObject root] == nil);
return isPersonAndRoot || isAccountAndRoot;
}];
So I need to convert this into a standard String format predicate, but I am unclear on how to check the entity type for the evaluated object. The Person
and Account
entities are subclasses of a Contact
entity which is the type being evaluated in the fetch request. I'm hoping it will see the sub-types.
Check the entity like this:
[NSPredicate predicateWithFormat:@"self.entity = %@", Person.entity];
Swift 4.0 way of checking for entity type using the entity property of the NSManagedObject as per malhal's suggestion.
let entityDescription = NSEntityDescription.entity(forEntityName: "Account", in: managedObjectContext)!
return NSPredicate(format: "entity = %@", entityDescription)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With