显示核心数据:如果属性具有相同的名称,则显示一次

2024-01-05

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

我有一个名为的核心数据实体材料我有属性category, 子类别 and 描述.

我有三个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;

这是我第一次尝试以这种方式排序和显示,我通常会使用关系的谓词,这是常见的做法,但为一组数据(材料)使用 3 个实体似乎不合逻辑。


您不需要为每个材料、类别和子类别提供三个不同的 NSMO。您只需要一个 NSMO,即具有这些属性类别、子类别和描述的材料。

在 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);

这样,您可以从核心数据中获取具有不同类别的所有材质 NSManagedObjects。

在 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);

这样,您可以从核心数据中获取具有不同类别的所有材质 NSManagedObjects。

对于第三个视图控制器列出属于所选类别和子类别的所有项目,请使用:

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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

显示核心数据:如果属性具有相同的名称,则显示一次 的相关文章

随机推荐