显示核心数据:如果属性具有相同名称显示一次 [英] Displaying Core Data: If attribute has same name display once

查看:114
本文介绍了显示核心数据:如果属性具有相同名称显示一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过许多谓词问题,我已经阅读了文档,没有任何似乎跳出来作为回答我的问题。



我有一个名为材料的核心数据实体,我有属性类别

我有三个 UITableViewControllers 使用谓词显示如下:



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



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



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



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



三个实体在一个核心数据模型中,我可以这样做吗?



我已尝试在 fetchedResultsController 方法中使用以下谓词代码,但可使用:

 材料* materials = [[Materials alloc] init]; //这不感觉它属于一个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:@MaterialsinManagedObjectContext:self.managedObjectContext];

//必需!除非你设置resultType为NSDictionaryResultType,distinct不能工作。
//后备存储中的所有对象都是隐式不同的,但两个字典可以重复。
//因为你只需要distinct类别,只需要'category'属性。
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@Category]];
fetchRequest.returnsDistinctResults = YES;

//现在应该在字典中产生一个不同值的NSArray。
NSArray * dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog(@names:%@,dictionaries);

有了这个,你可以从Core数据中获取所有具有不同类别的Materials NSManagedObjects。



在ViewController 2中显示子类别:

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


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

fetchRequest.predicate = predicate;
//必需!除非你设置resultType为NSDictionaryResultType,distinct不能工作。
//后备存储中的所有对象都是隐式不同的,但两个字典可以重复。
//因为你只需要不同的SubCategory,只需要'SubCategory'属性。
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = [NSArray arrayWithObject:[[entity propertiesByName] objectForKey:@SubCategory]];
fetchRequest.returnsDistinctResults = YES;

//现在应该在字典中产生一个不同值的NSArray。
NSArray * dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog(@names:%@,dictionaries);

有了这个,你可以从Core数据中获取所有具有不同类别的Materials NSManagedObjects。



要让第三个ViewController列出属于所选类别和子类别的所有项目,请使用:

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


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

fetchRequest.predicate = predicate;
//必需!除非你设置resultType为NSDictionaryResultType,distinct不能工作。
//后备存储中的所有对象都是隐式不同的,但两个字典可以重复。

//现在应该在字典中产生一个不同值的NSArray。
NSArray * dictionaries = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];
NSLog(@names:%@,dictionaries);


I have viewed numerous predicate questions, I have read the docs and nothing seems to be jumping out as an answer my problem.

I have a Core Data entity named Materials and I have the Attributes category, subcategory and description.

I have three UITableViewControllers and in each I want to use a predicate to display as follows:

TableViewController 1: Only the categories (no repeating of category names)

Select a category and go to TableViewController 2.

TableViewController 2: Display Subcategories (no repeating of subcategory names)

Select a subcategory and go to TableViewController 3 listing all items in that category and subcategory.

Without using three entities in a Core Data Model, can I do this?

I have tried using the following predicate code inside my fetchedResultsController method but to avail:

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;

This is the first time I have attempted to sort and display this way, I would usually use a predicate of a relationship as is common practice but it seemed illogical to have 3 entities for one set of data - Materials.

解决方案

You need not three different NSMO for each Materials, Category and Sub category. You only need one NSMO that is Materials which has these Attributes category, subcategory and description.

For Showing Categories in 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);

With this you can fetch all the Materials NSManagedObjects with distinct categories from Core data.

For Showing SubCategories in 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);

With this you can fetch all the Materials NSManagedObjects with distinct categories from Core data.

For third Viewcontroller to list all the items belong to a Selected category and subcategory use:

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

这篇关于显示核心数据:如果属性具有相同名称显示一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆