0

我有事件,每个 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 中获取具有特定类娱乐的事件呢?

4

1 回答 1

1

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

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

[fetchRequest setIncludesSubentities:NO];
于 2013-10-10T07:32:42.617 回答