OGeek|极客世界-中国程序员成长平台

标题: ios - 显示核心数据 : If attribute has same name display once [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 10:00
标题: ios - 显示核心数据 : If attribute has same name display once

我查看了许多谓词问题,我阅读了文档,但似乎没有什么可以作为我的问题的答案。

我有一个名为 Materials 的核心数据实体,我有属性 categorysubcategorydescription

我有三个 UITableViewControllers 并且在每个中我想使用一个谓词来显示如下:

TableViewController 1:只有类别(类别名称不重复)

选择一个类别并转到 TableViewController 2。

TableViewController 2:显示子类别(不重复子类别名称)

选择一个子类别并转到 TableViewController 3 列出该类别和子类别中的所有项目。

如果不在核心数据模型中使用三个实体,我可以这样做吗?

我尝试在我的 fetchedResultsController 方法中使用以下谓词代码,但成功了:

Materials * materials = [[Materials alloc]init];//this doesn't feel like it belongs inside a fetchedResultsController

NSPredicate * predicate = [NSPredicate predicateWithFormat"category == %@", materials.category];

fetchRequest.predicate = predicate;

这是我第一次尝试以这种方式排序和显示,我通常会按照惯例使用关系谓词,但为一组数据( Material )设置 3 个实体似乎不合逻辑。



Best Answer-推荐答案


您不需要为每个 Material 、类别和子类别设置三个不同的 NSMO。您只需要一个 NSMO,它是具有这些属性类别、子类别和描述的 Material 。

在 ViewController 1 中显示类别:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName"Materials"];
NSEntityDescription *entity = [NSEntityDescription entityForName"Materials" inManagedObjectContext:self.managedObjectContext];

// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work. 
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct Categories, only ask for the 'Category' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey"Category"]];
fetchRequest.returnsDistinctResults = YES;

// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);

通过它,您可以从核心数据中获取具有不同类别的所有 Material NSManagedObject。

用于在 ViewController 2 中显示子类别:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName"Materials"];
NSEntityDescription *entity = [NSEntityDescription entityForName"Materials" inManagedObjectContext:self.managedObjectContext];


NSPredicate * predicate = [NSPredicate predicateWithFormat"SELF.category == %@", selectedCategoryName];

fetchRequest.predicate = predicate;
// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work. 
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.
// Since you only want distinct SubCategory, only ask for the 'SubCategory' property.
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey"SubCategory"]];
fetchRequest.returnsDistinctResults = YES;

// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);

通过它,您可以从核心数据中获取具有不同类别的所有 Material NSManagedObject。

让第三个 Viewcontroller 列出属于 Selected 类别和子类别的所有项目:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName"Materials"];
NSEntityDescription *entity = [NSEntityDescription entityForName"Materials" inManagedObjectContext:self.managedObjectContext];


NSPredicate * predicate = [NSPredicate predicateWithFormat:@"SELF.category == %@ and SELF.subcategory == %@", selectedCategoryName,selectedSubcatgory];

fetchRequest.predicate = predicate;
// Required! Unless you set the resultType to NSDictionaryResultType, distinct can't work. 
// All objects in the backing store are implicitly distinct, but two dictionaries can be duplicates.

// Now it should yield an NSArray of distinct values in dictionaries.
NSArray *dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog (@"names: %@",dictionaries);

关于ios - 显示核心数据 : If attribute has same name display once,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27538930/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://sqlite.in/) Powered by Discuz! X3.4