CoreData:如何获取子类对象和超类对象

时间:2019-03-14 15:29:52

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

我有一个名为Geography的核心数据超级实体,该实体具有属性

Geography: NSManagedObject {
         NSString: name,
         NSString: id,
}

它是下面的子类

State: Geography {
}

MSA: Geography {
}

Territory: Geography {
     NSString: formularyTemplate
}

self.entityName = @"Geography";

- (NSFetchedResultsController *)fetchedResultsController {
    if (_fetchedResultsController == nil && self.entityName && self.filterKey) {
        NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:self.entityName];
        fetchRequest.predicate = [self filterPredicate];

        NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:self.filterKey ascending:YES];
        fetchRequest.sortDescriptors = @[sortDescriptor];
        _fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.context sectionNameKeyPath:nil cacheName:nil];
    }
    return _fetchedResultsController;
}

我想要一个谓词来获取所有地理区域(表示州,MSA,领土),但是应该根据特定的ID过滤领土,我不希望所有领土

NSMutableArray *predicates = [NSMutableArray new];

NSPredicate *filterTextPredicate = [NSPredicate predicateWithFormat:@"%K contains[c] %@", self.filterKey, trimmedFilterText];

NSPredicate *territoryPredicate = [NSPredicate predicateWithFormat:@"(formularyTemplate != nil) && formularyTemplate == %@", @"34"];

 [predicates addObject: filterTextPredicate];
 [predicates addObject: territoryPredicate];

 [NSCompoundPredicate andPredicateWithSubpredicates:predicates];

但是它不起作用。

0 个答案:

没有答案