MaximusAlarmus
MaximusAlarmus

Reputation: 147

NSPredicate for detecting Class of the property

I have events, each Event has to-one relationship with NSManagedObject Entertainment, and Entertainment has to-many relationship, and has NSSet of events

@interface Event : NSManagedObject
@property (nonatomic, retain) NSDate *creationDate;
@property (nonatomic, retain) NSDate * date;
@property (nonatomic, retain) NSString * objectId;
@property (nonatomic, retain) NSString * price;
@property (nonatomic, retain) Entertainment *entertainment;
@property (nonatomic, retain) Place *place;
@property (nonatomic, retain) NSString *townName;
- (void)fillProperties:(NSDictionary *)JSON;
@end

@interface Entertainment : NSManagedObject
@property (nonatomic, retain) NSString * additionalInfo;
@property (nonatomic, retain) NSNumber * allCommentsLoaded;
@property (nonatomic, retain) id image;
@property (nonatomic, retain) NSString * imageURL;
@property (nonatomic, retain) NSString * info;
@property (nonatomic, retain) NSString * name;
@property (nonatomic, retain) NSString * objectId;
@property (nonatomic, retain) NSSet *comments;
@property (nonatomic, retain) NSSet *events;
- (void)fillProperties:(NSDictionary *)JSON;
@end

Entertainment have many subclasses So I need to create such NSFetchedResultController for getting Events, that have Entertainment of specific class

I tried use

- (NSFetchedResultsController *)fetchedResultsControllerForEventsFromEntertainment:(Class)className 
{
    NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:NSStringFromClass([Event class])];
    fetchRequest.predicate = [NSPredicate predicateWithFormat:@"entertainment isMemberOfClass: %@", [className class]];
    NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"objectId" ascending:YES];
    fetchRequest.sortDescriptors = @[sortDescriptor];
    fetchRequest.fetchBatchSize = 20;
    NSFetchedResultsController *fetchResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
    return fetchResultsController;
}

But I get mistake:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
 reason: 'Unknown/unsupported comparison predicate operator type'
***

I read, that when we work with SQLite, this method isMemberOfClass not work, because SQLite don't know such method. In usual NSPredicate for example simple array it must work.

So how can I fetch events from SQLite, that have Entertainment of specific class?

Upvotes: 0

Views: 521

Answers (1)

Martin R
Martin R

Reputation: 539725

It is not possible to refer to the entity name or class name in the predicate of a (SQLite based) Core Data fetch request.

But to fetch only objects of the entity specified in the fetch request, and not objects of any subentity, just set

[fetchRequest setIncludesSubentities:NO];

Upvotes: 1

Related Questions