NSFetchedResultsController和使用关系的表 [英] NSFetchedResultsController and Tables using Relationships

查看:57
本文介绍了NSFetchedResultsController和使用关系的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

举一个简单的例子,我有两个具有多对多关系的实体。第一个实体称为项目,第二个称为员工。该项目实体可以有1个或多个雇员,而雇员可以在一个或多个项目中。



同样为了简单起见,项目具有一个名为projectName的属性,并且与Employee之间存在a to many关系叫withEmployees。相反,Employee具有employeeName,并且与名为myProjects的项目有很多关系。



使用NSFetchedResultsController我想要一个表,该表将具有projectName作为节名和行的employeeName。使用Project作为获取的实体,我可以轻松地获取每个项目并创建适当的部分名称。但是,在计算numberOfRowsInSection时会出现问题。由于取得的结果拉回了该项目的一条记录,因此,rowsrows仅看到一个实体,因为它没有查看.withEmployees关系中找到的NSSet。这意味着将只输出一名雇员,而不是NSSet关系中的人数计数。



在表中使用一对多关系如何最好地克服这个问题?



谢谢。 p>

以下是fetchedcontroller的代码:

  NSFetchRequest * fetchRequest = [ [NSFetchRequest alloc] init]; 
NSEntityDescription *实体= [NSEntityDescription entityForName:@ Projects
inManagedObjectContext:self.managedObjectContext];

[fetchRequest setEntity:entity];

NSSortDescriptor * firstSort = [[NSSortDescriptor alloc] initWithKey:@ projectName
升序:否];

NSArray * sortDescriptors = @ [firstSort];

[fetchRequest setSortDescriptors:sortDescriptors];


_fetchedController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:@ projectName
cacheName:nil];


解决方案

我终于找到了答案。如上所述,我拥有的实体关系都很好。问题再次出在通过多对多关系返回正确的行数。我以以下方式解决了这个问题:

 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{

Projects * project = [self.fetchedController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

回报[project.withEmployees count];

}

在上面的内容中,我使用了适当的部分,因为我知道因为总会有一行包含一组未知大小,所以我创建了一个索引路径,该路径查找每个部分的一个且只有一个标头记录(项目)。然后,我只是查看构成该关系的NSSet并简单地返回该计数。现在我们可以得到每节正确的行数。



关于单元格本身:

 项目*项目= [self.fetchedController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:atIndex.section]]]; 

UITableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@ Cell forIndexPath:atIndex];

NSArray * empArray = [project.withEmployees allObjects];

员工*员工= [empArray objectAtIndex:atIndex.row];

cell.textLabel.text = employee.firstName;

}

这里的关键是获取一个部分的记录,然后提取构成多对多关系的所有数据。同样,我需要创建一个索引路径以便仅获取父记录。现在,使用行可以遍历数组以获取每个元素。


As a simple example lets say I have 2 entities with a many to many relationship. The first entity is called "Projects" and the second "Employees". The project entity can have 1 or more employees and employees can be on one or more projects.

Again for simplicity Project has an attribute called projectName and a to many relationship with Employee called withEmployees. Conversely Employee has employeeName and a to many relationship to projects called myProjects.

Using NSFetchedResultsController I want a table that will have projectName as the section name and employeeName for the the rows. Using Project as the fetched entity I can easily get each project and create the appropriate section name. However, the issue comes when calculating the numberOfRowsInSection. As the fetched results pulled back one record for the project the numberofrows sees only one entity as it doesn't look into the NSSet found in the .withEmployees relationship. This means that only one employee will be output rather than the count of the number within the NSSet relationship.

How is this issue best overcome to use a to many relationship within a table?

Thanks.

Here is the code for the fetchedcontroller:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Projects" 
     inManagedObjectContext:self.managedObjectContext];

[fetchRequest setEntity:entity];

NSSortDescriptor *firstSort = [[NSSortDescriptor alloc] initWithKey:@"projectName"
                                                                      ascending:NO];

NSArray *sortDescriptors = @[firstSort];

[fetchRequest setSortDescriptors:sortDescriptors];


_fetchedController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest
                                                                    managedObjectContext:self.managedObjectContext
                                                                      sectionNameKeyPath:@"projectName"  
                                                                                 cacheName:nil];

解决方案

I have finally found the answer. The entity relationships I had were all fine as described above. The issue again was to get the correct number of rows returned through the to many relationship. I solved that in the following manner:

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    Projects *project = [self.fetchedController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:section]];

return [project.withEmployees count];

}

With the above I used the appropriate section and since I know that there is always one row that contains a set of unknown size, I created an indexpath that looked to the one and only one header record (the project) for each section. I then just looked into the NSSet that makes up the relationship and simply return the count of that. Now we get the correct number of rows per section.

As for the cells themselves:

Projects *project = [self.fetchedController objectAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:atIndex.section]];

UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:atIndex];

NSArray *empArray = [project.withEmployees allObjects];

Employee *employee = [empArray objectAtIndex:atIndex.row];

cell.textLabel.text = employee.firstName;

}

The key here was to get the one section record and then pull out all of the data that makes up the to many relationship. Again I need to create an indexpath in order to only get the parent record. Now using the rows I can iterate through the array to get each element.

这篇关于NSFetchedResultsController和使用关系的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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