0

我有事件,每個事件具有NSManagedObject娛樂對一的關係,及娛樂具有一對多的關係,並且具有事件的NSSetNSPredicate用於檢測的屬性的類

@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中獲取具有特定類娛樂的事件?

+0

的可能的複製[NSPredicate過濾掉子結果(https://開頭stackoverflow.com/questions/16636338/nspredicate-that-filters-out-subclass-results) –

回答

1

在謂詞 a(基於SQLite的)核心數據提取請求中,不可能引用實體名稱或類名稱。

但獲取獲取請求指定的實體的唯一對象,而不是對象的任何子實體 ,只需設置

[fetchRequest setIncludesSubentities:NO];