NSPredicate用于检测属性的类 [英] NSPredicate for detecting Class of the property

查看:46
本文介绍了NSPredicate用于检测属性的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有事件,每个事件与NSManagedObject Entertainment都具有一对一的关系,而Entertainment与一对多的关系也具有NSSet的事件

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

娱乐有许多子类因此,我需要创建这样的NSFetchedResultController来获取具有特定类的娱乐功能的事件

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

我尝试使用

- (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;
}

但是我弄错了:

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

我读到,当我们使用SQLite时,此方法 isMemberOfClass 不起作用,因为SQLite不知道这种方法.例如,在通常的NSPredicate简单数组中,它必须起作用.

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.

那么我该如何从具有特定类娱乐性的SQLite中获取事件?

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

推荐答案

不可能在谓词中引用实体名称或类名称(基于SQLite的)Core Data提取请求.

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

这篇关于NSPredicate用于检测属性的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆