Ben Thompson
Ben Thompson

Reputation: 4833

Using NSPredicate to filter on both Core attribute and entity

I have two entities in core data (call them entityOne and entityTwo). I have a entityOne<--->>entityTwo relationship between them.

I am now trying to code an NSPredicate to fetch entityTwo objects on the following basis:

  1. Fetch entityTwo objects that have a relationship with a specified entityOne object.

  2. Filter out those entityTwo objects which have no value for one of their attributes.

I am best doing both of these in an NSPredicate or is there a better way?

I am trying the following:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(attribute <> "") AND (relationship == entityOne"];
    [request setPredicate:predicate];

Any pointers on coding great fully received.

Upvotes: 1

Views: 332

Answers (1)

Nikita Pestrov
Nikita Pestrov

Reputation: 5966

You can use a Predicate like this:

[NSPredicate predicateWithFormat:@"entityOneRelationship = %@ AND attribute.length > 0",specifiedEntityOne];

Pretty common approach to do that.

Upvotes: 2

Related Questions