Question

I am trying to use a predicate to get data where the data i want to search on is in a NSSet

@property (nonatomic, retain) NSSet *categories;
@property (nonatomic, retain) NSString *otherDetails;
@property (nonatomic, retain) NSNumber *id;

the NSSet categories is made up from another entity and contains:

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSNumber *id;

then try getting this via a predicate

NSFetchRequest *fetcher =[NSFetchRequest fetchRequestWithEntityName:@"MyEntity"];
fetcher.predicate = [NSPredicate predicateWithFormat:@categories in  %@", catList];

where catList is:

NSmutableArray *catList = [[NSMutableArray alloc] init];

and is populated with data

this is then giving me the following error

'NSInvalidArgumetException', reason: 'unimplemented SQL generation for predicate: (categories IN {"this", "that"});

I tried changing it to use the NSSet fields like such

fetcher.predicate = [NSPredicate predicateWithFormat:@categories.name in  %@", catList];

but this didn't work.

if i change the predicate to use the NSString it does not error but as the data is different no results are returned.

fetcher.predicate = [NSPredicate predicateWithFormat:@categories in  %@", catList];

So what must I do to use the predicate to do the query on the NSSet?

Update: I have since found this How to check if an objects is inside of a NSSet of objects by using predicateWithFormat in CoreData? that would possibly solve the question posted, but i have simpliefied this a little too much.

what i actually have is another entity with a whole heap of details I am search on that then has a many to one relationship with my "MyEntity" entity.

MySearchEntity:

@property (nonatomic, retain) NSNumber *searchField1;
@property (nonatomic, retain) NSNumber *searchField2;
@property (nonatomic, retain) MyEntity *myEntity;
@property (nonatomic, retain) NSNumber *id;

and a fetchrequest like this

NSFetchRequest *fetcher =[NSFetchRequest fetchRequestWithEntityName:@"MySearchEntity"];
fetcher.predicate = [NSPredicate predicateWithFormat:@"myEntity.otherDetails CONTAINS %@ && searchField1 <= %f && searchField2 >= %f && myEntity.categories.name in  %@", catList];

so the rest of the statement works but i am still unable to get filter it by the MyEntities many to many relationship with categories.

Était-ce utile?

La solution

To find the MySearchEntity objects where the related MyEntity object has at least one category in the given set, you would use the predicate

NSArray *catList = array of Category objects;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY myEntity.categories IN %@", catList];

or, if the categories are given by name:

NSArray *catList = array of NSString objects;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY myEntity.categories.name IN %@", catList];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top