iOS Developer
iOS Developer

Reputation: 1723

Getting the results from core data based on a particular attribute in objective c

In my project, I implemented core data to save the bulk data under several entities. One of entity is "coupon" and it have nearly 10 attributes. One is username. When the app opens I need to fetch all the entries based on a particular user who logged in. Username is already saved into the core data when the user enters each entry. How can I set the predicate for this? I am new to core data concept. This is how i am fetching the all results under the entity coupon.

NSPredicate *pred = [NSPredicate predicateWithFormat:@"Category BEGINSWITH[c] @"All" and Used == NO"];
[request setPredicate: pred];

NSArray *objects = [context  executeFetchRequest:request error:&error];
NSLog(@"%@arry",objects);

if ([objects count]>0) {
    [self.couponList addObjectsFromArray:objects];
}

[request release];

Any help or suggestion will be appreciated.Thanks in advance!

Upvotes: 0

Views: 247

Answers (1)

Mundi
Mundi

Reputation: 80265

Your predicate has wrong syntax. You need to substitute the parameters into the format string.

Also, your predicate seems to have nothing to do with the username. Why?

[NSPredicate predicateWithFormat:@"username = %@", userName]; 
// or, including your existing predicate:
[NSPredicate predicateWithFormat:@"username = %@ && 
                                   category BEGINSWITH[c] %@ && 
                                   used == %@",
   userName, @"All", [NSNumber numberWithBool:NO]];

Note that I have used category and used with lower initial letters. I assume these are properties of your entity in being fetched, so by convention the first letter should be small.

Also, you could actually also use your version used == NO if you prefer.

Upvotes: 1

Related Questions