How to create a fetch request to fetch objects with specific attribute values

91 views Asked by At

I want to create a fetch request so I can fetch objects with specific attribute values.

In one of my view controllers I use a fetch request that looks like this:

- (NSFetchRequest *)targetsFetchRequest {

    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Target"];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"order" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];
    return fetchRequest;
}

But now I have created another attribute to the target object which called "status" and in another view controller I want to create a fetch request to fetch only the target objects that their status is equal to "1"...

Please help me to figure out how to create this fetch request.

Thanks allot!

1

There are 1 answers

0
pbasdf On

You need to add a predicate to your fetch request. Take a look at the Apple Documentation for NSPredicate and the Predicate Programming Guide.

In your case you need something like:

fetchRequest.predicate = [NSPredicate predicateWithFormat:@"status == 1"];