NSPredicate for detecting Class of the property

江枫思渺然 提交于 2019-12-11 20:01:37

问题


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?


回答1:


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];


来源:https://stackoverflow.com/questions/19289185/nspredicate-for-detecting-class-of-the-property

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!