I am using [fetchRequest setPredicate:[NSPredicate predicateWithValue:YES]]; to debug my code to by pass universal true predicate.
When I exicuting lldb command po [NSPredicate predicateWithValue:YES], it print TRUEPREDICATE.
The return type for predicateWithValue: is NSPredicate.
When I changed it to [fetchRequest setPredicate:TRUEPREDICATE]; Xcode complain a syntax error "use of un-declared identifier"
My question is should I have to import any header file to remove error? If not then how TRUEPREDICATE differ from [NSPredicate predicateWithValue:YES].
A universal true predicate can be created with
[NSPredicate predicateWithValue:YES];
or
[NSPredicate predicateWithFormat:#"TRUEPREDICATE"];
Both statements return the same predicate.
po prints the description of an object. The description of NSPredicate is similar to the format.
in Swift 3 you would do as below
NSPredicate(value: true)
or
NSPredicate(format: "TRUEPREDICATE")
Related
- (void)filterViaCategories:(NSArray *)array {
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"(todo_category_id == %#)" argumentArray:array];
}
but when I have used:
po predicate
Result is:
todo_category_id == 41123..
It just using 41123 from zero index element of array
i want all categories from data base for all id present in array not the only object at index zero:
(todo_category_id == 41123, 41234, 33455) etc.
How can I do this?
Then you should not be using predicateWithFormat:argumentArray: and you need to change your format.
You want something like:
NSPredicate *predicate = [NSPredicate predicateWithFormat:#"todo_category_id IN %#", array];
I have two Entities in CoreData called User and Coupon, they are in Many-to-Many relationship. I wanted to fetch for all Coupons except those owned by user.userId = 1, where userId is NSString.
I used:
[NSPredicate predicateWithFormat:#"NOT(ANY couponOwners.userId = %#)", #"4"];
to be the predicate of my fetchedResultsController
but not filtering with correct results. One of the User in couponOwners of the Coupon is still having userId = 4.
Could somebody please help? I have been stuck for quite a while. Thanks in advance.
Core Data predicates with "NOT ANY" do not work (that seem to be a Core Data bug). Actually
[NSPredicate predicateWithFormat:#"NOT(ANY couponOwners.userId = %#)", #"4"];
returns the same result set as
[NSPredicate predicateWithFormat:#"ANY couponOwners.userId != %#", #"4"];
which is of course wrong. As a workaround, you can use a SUBQUERY:
[NSPredicate predicateWithFormat:#"SUBQUERY(couponOwners, $c, $c.userId == %#).#count == 0", #"4"]
I'm working on a recipe book right now using Core Data. It's the first time I'm using CoreData and it's working so far, but I'm having some trouble using it in the iPad's split view.
Here's my object model:
http://i621.photobucket.com/albums/tt295/Naosu_FFXI/ObjectModel.png
In my app, steps and ingredients are shown in two tables in the detail view. When I have one recipe, it works as expected. However, the NSFetchedResultsControllers for both tables pulls all the information regardless of what recipe you select. So using an NSPredicate seems to be the most obvious choice.
Here is my code snippet for the NSPredicate:
filteredIngredientsArray = [[NSMutableArray alloc] init];
.... snip ....
//---------- Filtering the ingredients by name ----------
NSError *error = nil;
NSPredicate *ingredientsPredicate = [NSPredicate predicateWithFormat:#"recipe.recipeName == '%#'", selectedName];
[fetchRequest setPredicate:ingredientsPredicate];
NSLog(#"Filtering the INGREDIENTS with this: %#", selectedName);
NSArray *loadedIngredients = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];
filteredIngredientsArray = [[NSMutableArray alloc] initWithArray:loadedIngredients];
[self.ingredientTable reloadData];
When I use this, my tables don't get filled period.... so it's definitively working, but not working as I want it to. My NSLog shows that the value of the selectedName variable is the name of the recipe the user taps on.
This has been driving me up the wall and it really bothers me, so any feedback/help would be appreciated.
Remove the single quotes around %# in the predicate format:
[NSPredicate predicateWithFormat:#"recipe.recipeName == %#", selectedName]
I have an App with CoreData and I need to use NSPredicate to retrieve all the contacts from a city.
The question is that "city" maybe written like LONDON or London or even london. And the user will type London to search, or LoNDon.
What I need is to use NSPredicate with localizedCaseInsensitiveCompare, so that all the records are retrieved.
Code:
request.predicate = [NSPredicate predicateWithFormat:
#"activityDeleted == %# && (SUBQUERY(hasMembers, $sub, $sub.memberDeleted == %#).#count > 0) && (SUBQUERY(hasMembers, $sub, $sub.city == %#)",
[NSNumber numberWithBool:NO],
[NSNumber numberWithBool:NO],
city];
The above NSPredicate retrieves all the groups that have Contacts, not deleted, that are in a certain City, but I have the CaseInsensitive problem...
How can I do that?
Thanks,
RL
You don't need subquery here. Your entire predicate can instead be:
#"activityDeleted == NO AND ANY hasMembers.memberDeleted = NO AND ANY hasMembers.city =[cd] %#", city
You can use the case insensitive and diacritic insensitive modifiers "[cd]" on the ==
SUBQUERY(hasMembers, $sub, $sub.city ==[cd] %#)
You might try searching stackoverflow for "case insensitive nspredicate" there are several answers already https://stackoverflow.com/search?q=case+insensitive+nspredicate
I've searched a lot here and couldn't find a solution to my situation:
I have a TableViewController that uses NSFetchedResultsController to display data from CoreData.
The model has an entity "Places" that has a to-many relationship called "Types" (and an inverse one, also to-many relationship).
In a first TableViewController I display the objects from entity "types" (each place can belong to more than one type, and one type can have more than one place). When the user taps on a row it calls a new TableViewController that will show objects from entity "Places" related to "Types" using a NSFetchedResultsController.
I know I could just use:
NSSet = [aType valueForKey:#"Places"];
However, I really want to use the NSFetchedResultsController and all its benefits.
Well, at the NSFetchedResultsController accessor method I was able to recreate this relationship by using:
NSEntityDescription *entity = [NSEntityDescription entityForName:#"Places"
inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
NSDictionary *types = [NSDictionary dictionaryWithObjectsAndKeys:self.theTypes, #"TYPES", nil];
NSPredicate *predicateAny = [NSPredicate
predicateWithFormat:#"ANY types IN $TYPES"];
NSPredicate *predicate = [predicateAny predicateWithSubstitutionVariables:types];
This code works fine as it returns "Places" that are related to the "Types" I want (those were hold inside the property theTypes). I use a property (theTypes), in the TableViewController to hold all "Types" objects one selected at the original tableViewController.
The problem is that the entity "Places" has a property named "distance" that I also need to use as a filter inside the NSPredicate, like this:
NSNumber *radious = [NSNumber numberWithDouble:10000.00];
NSPredicate *pred = [NSPredicate predicateWithFormat:#"distance BETWEEN {0 , %#}", radious];
NSDictionary *types = [NSDictionary dictionaryWithObjectsAndKeys:self.theTypes, #"TYPES", nil];
NSPredicate *predicateAny = [NSPredicate
predicateWithFormat:#"ANY types IN $TYPES"];
NSPredicate *predicate = [predicateAny predicateWithSubstitutionVariables:types];
NSPredicate *thePred = [NSCompoundPredicate andPredicateWithSubpredicates:
[NSArray arrayWithObjects:predicate, pred, nil]];
[fetchRequest setPredicate:thePred];
Here's where the problem occurs: the "distance" filter appears to be simply ignored by the predicate at the fetchRequest. The resulted fetch always has Places that don't match the #"distance BETWEEN {0 , %#} clause.
Can anyone help me figure out what I am missing here?
Thanks a lot!
Daniel
This predicate:
NSPredicate *predicateAny = [NSPredicate
predicateWithFormat:#"ANY types IN $TYPES",
radious];
Doesn't make sense because you don't use the radious variable in this predicate. Not sure if that is the source of the problem but you should clean it up anyway.
This predicate:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"distance BETWEEN {0 , %#}", radious];
... could probably be:
NSPredicate *pred = [NSPredicate predicateWithFormat:#"distance <=%#", radious];
... unless you have the possiblity of a negative radious value. It will be much faster that way.