Devarshi
Devarshi

Reputation: 16758

NSPredicate to filter records after calculating value of an attribute

Generally we use NSPredicate like this:

NSPredicate *matchName = [NSPredicate predicateWithFormat:
                             @"firstName == %@",@"Miraaj"];

Is there any to achieve something like this:

NSPredicate *matchNameHash = [NSPredicate predicateWithFormat:
         @"hash(firstName) == %@",hash(@"Miraaj")];

ie. it will filter records after matching hash of value of an attribute.

Upvotes: 0

Views: 106

Answers (1)

Mundi
Mundi

Reputation: 80271

According to the Predicate Programming Guide, there is no function hash available in core data / SQLite.

What attribute of an entity is supposed to be a hash anyway? In Core Data, you should use relationships to other entities with properties, not "hashes". What you need is something like

[NSPredicate predicateWithFormat:
        @"hashProperty.name = %@", localNameHash[@"Miraaj"];

Assuming you want to compare all attributes of some "Person" entity (you are using first name), you can check for the presence of an instance of your subclass in the persistent store like this

[NSPredicate predicateWithFormat:
        @"person = %@", miraajPersonObject];

Upvotes: 1

Related Questions