2

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 Answer 1

1

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"];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.