【问题标题】:NSPredicate for detecting Class of the property用于检测属性类别的 NSPredicate
【发布时间】:2013-10-10 07:05:56
【问题描述】:

我有事件,每个 Event 和 NSManagedObject Entertainment 是一对一的关系,而 Entertainment 有一对多的关系,并且有 NSSet 的事件

@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 来获取具有特定类娱乐的事件

我试过了

- (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 中,例如简单的数组,它必须工作。

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

【问题讨论】:

标签: ios objective-c core-data nspredicate nsfetchedresultscontroller


【解决方案1】:

在谓词中不能引用实体名或类名 一个(基于 SQLite 的)核心数据获取请求。

但要仅获取获取请求中指定的实体的对象,而不获取对象 任何子实体,只需设置

[fetchRequest setIncludesSubentities:NO];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 2020-03-09
    • 1970-01-01
    • 2012-07-13
    • 1970-01-01
    • 2013-04-08
    • 2021-10-25
    相关资源
    最近更新 更多