Ios NSFETCH请求行中的子对象

Ios NSFETCH请求行中的子对象,ios,core-data,nsfetchedresultscontroller,nsfetchrequest,Ios,Core Data,Nsfetchedresultscontroller,Nsfetchrequest,我的数据模型如下: 类别 ->名字 ->项目(类型为“Car”的项目数组) 我试图以节作为名称获取,行应该是items(这是一个NSArray) 这是我的代码,我希望返回行作为Car,但它仍然返回“Category”。那么,什么是NSFETCH请求 NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Categories"]; NSSortDescriptor *sortDesc=[[NSSortDescr

我的数据模型如下: 类别

->名字

->项目(类型为“Car”的项目数组)

我试图以节作为名称获取,行应该是items(这是一个NSArray)

这是我的代码,我希望返回行作为Car,但它仍然返回“Category”。那么,什么是NSFETCH请求

NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Categories"];


NSSortDescriptor *sortDesc=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDesc]];

fetchController=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:delegate.managedContext sectionNameKeyPath:@"name" cacheName:nil];
NSError *fetchError;
[fetchController performFetch:&fetchError];
我附上了模型的图片

取车代码如下

AppDelegate *del=(AppDelegate*)[UIApplication sharedApplication].delegate;
NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Car"];


NSSortDescriptor *sortDesc=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDesc]];

fetchController=[[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:del.managedContext sectionNameKeyPath:@"Categories.name" cacheName:nil];
NSError *fetchError;
[fetchController performFetch:&fetchError];
代码上传到这里

注意:-我想显示空部分,因为我有标题视图,其中有“添加”按钮。因此,用户可以很容易地知道类别的数量

为了实现上述“注意事项”中提到的事情,我尝试了以下方法。我已经创建了一个
CarViewController2
。它创建的节数与类别数相等,但行数取决于
类别关系
(这是一个数据集)。可以通过在ViewController.m的发送者中取消注释一些代码来测试这一点。(取消注释一次,然后再次注释)代码已更新,可以下载


如果需要,请更正代码。谢谢。

您的设置应该是:

NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Car"];
NSSortDescriptor *sortDesc=[[NSSortDescriptor alloc] initWithKey:@"category.name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDesc]];

fetchController=[[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                    managedObjectContext:delegate.managedContext 
                                                      sectionNameKeyPath:@"category.name" 
                                                               cacheName:nil];
category.name
假设
category
项目的反向关系是
Car
category
(一对多关系)

请注意,您可能希望将排序描述符更改为按某个
Car
属性排序

编辑:

发布模型和当前代码后,您不能按
类别
名称对数据进行分区,因为这是一种多对多关系。
不同部分不能有重复项。

如果
Car
“c”属于类别“g1”和“g2”,您需要自己构建数据结构,或者引入一个中介实体,在Car和类别之间建立链接,这将是请求的基础。

编辑2:

在项目中,您将
关联设置为一个
类别
实体,这将允许您形成以下请求:

NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Car"];
NSSortDescriptor *sortDesc=[[NSSortDescriptor alloc] initWithKey:@"carName" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDesc]];

fetchController=[[NSFetchedResultsController alloc] initWithFetchRequest:request
                                                    managedObjectContext:delegate.managedContext 
                                                      sectionNameKeyPath:@"carRelation.categoryName" 
                                                               cacheName:nil];
这仍然不会显示没有汽车的类别(因为您有一个汽车视图和一个类别视图)。

编辑3:

您对
CarViewController.m
的代码更正:

-(void) getData{
    AppDelegate *del=(AppDelegate*)[UIApplication sharedApplication].delegate;
    NSFetchRequest *req=[[NSFetchRequest alloc] initWithEntityName:@"Car"];
    NSSortDescriptor *descriptor1=[[NSSortDescriptor alloc] initWithKey:@"carRelationship.categoryName" ascending:YES];
    NSSortDescriptor *descriptor2=[[NSSortDescriptor alloc] initWithKey:@"carName" ascending:YES];
    [req setSortDescriptors:@[descriptor1,descriptor2]];
    fetchController=[[NSFetchedResultsController alloc] initWithFetchRequest:req managedObjectContext:del.managedContext sectionNameKeyPath:@"carRelationship.categoryName" cacheName:nil];
    NSError *fetchError;
    fetchController.delegate=self;
    [fetchController performFetch:&fetchError];
    NSLog(@"Fetch ERROR= %@",fetchError);
}

- (NSString*) tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    id<NSFetchedResultsSectionInfo> sectionInfo=[[fetchController sections] objectAtIndex:section];
    return [sectionInfo name];
}
-(void)获取数据{
AppDelegate*del=(AppDelegate*)[UIApplication sharedApplication]。委托;
NSFetchRequest*req=[[NSFetchRequest alloc]initWithEntityName:@“Car”];
NSSortDescriptor*描述符1=[[NSSortDescriptor alloc]initWithKey:@“carRelationship.categoryName”升序:是];
NSSortDescriptor*描述符2=[[NSSortDescriptor alloc]initWithKey:@“carName”升序:是];
[req setSortDescriptors:@[descriptor1,descriptor2];
fetchController=[[NSFetchedResultsController alloc]initWithFetchRequest:req managedObjectContext:del.managedContext sectionNameKeyPath:@“carRelationship.categoryName”cacheName:nil];
n错误*fetchError;
fetchController.delegate=self;
[fetchController performFetch:&fetchError];
NSLog(@“Fetch ERROR=%@”,fetchError);
}
-(NSString*)表格视图:(UITableView*)表格视图标题标题标题部分:(NSInteger)部分
{
id sectionInfo=[[fetchController sections]objectAtIndex:section];
返回[sectionInfo name];
}
正如我之前解释的,这仍然不会显示没有汽车的类别

让我们试试看

NSFetchRequest *request=[[NSFetchRequest alloc] initWithEntityName:@"Category"];
NSSortDescriptor *sortDesc=[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sortDesc]];
fetchController=[[NSFetchedResultsController alloc] initWithFetchRequest:request   managedObjectContext:delegate.managedContext sectionNameKeyPath:@"name" cacheName:nil];
NSError *fetchError;
[fetchController performFetch:&fetchError];

NSArray *categories = [fetchedResultsController fetchedObjects];
for(Category *c in categories){
   NSSet *items = c.items;// array that you want
}

performFetch填充数组,使用fetchController设置节数和行数。您提供的解决方案无法使用我的现有代码。@andyPaul:performFetch返回BOOL而不是数组。NSArray*categories=[fetchedResultsController FetchedObject]->您得到哪些类别?什么数组?我确实添加了关系,但它没有更新核心数据模型类,所以我必须删除并重新创建该类。如果“Car”实体为空,它将显示分区列表。由于存在实体“类别”中仍然存在的类别(如SUV和跑车等)。你能举个例子吗?@andyPaul这不会导致空部分(类别中没有汽车)。使用关系重新生成类(尽管将其添加到实体就足够了),并在添加
Car
对象时进行设置。您想要什么类型的示例?我已经编辑了代码,请告诉我是否需要指定任何其他内容来解决它。@andyPaul您应该按
carName
属性排序,该属性是在
Car
实体中定义的属性。它在获取时崩溃。