核心数据 - 从ManyToMany关系构建NSPredicate [英] Core data - Building a NSPredicate from a ManyToMany relationship

查看:169
本文介绍了核心数据 - 从ManyToMany关系构建NSPredicate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我对上一个问题的后续问题:



核心数据:以多对多关系管理员工合同?



有关于这个问题的图表,作为一个快速提醒,有以下内容:



contract> - employees



我已经能够在每个实体中手动保存1个实体,并在NSLog中进行了验证。



我创建了一个CompanyListPage列出所有公司。这个想法是,当你点击一个公司,你会得到一个列表,列出所有与该公司有合同的员工。



如下图所示: p>

 公司:
名称:A

合同:
length:33 wks
salary:20000

员工:
名称:Bob Jones

在我的CompanyListPage的didSelectRowAtIndex页面中有以下内容。

   - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


Company * c = [fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@您点击了%@,c.name);
NSString * companyName = c.name;

EmployeesListPage * employeesListPage = [[EmployeesListPage alloc] initWithNibName:@EmployeesListPagebundle:[NSBundle mainBundle]];

[self.navigationController pushViewController:employeesListPage animated:YES];

employeesListPage.title = companyName;
employeesListPage.managedObjectContext = self.context;
employeesListPage.managedObject = c;

[superstarsList release];

}

问题是,我不知道我的NSPredicate应该看起来像当我最终去到employeesListPage。



此时,它:

 {

if(fetchedResultsController!= nil){
return fetchedResultsController;
}

//创建和配置读取请求
NSFetchRequest * fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription * entity = [NSEntityDescription entityForName:@EmployeesinManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];

//创建排序描述符数组
NSSortDescriptor * authorDescriptor = [[NSSortDescriptor alloc] initWithKey:@nameascending:YES];
NSArray * sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor,nil];
[fetchRequest setSortDescriptors:sortDescriptors];

//创建并初始化fetch结果控制器
NSFetchedResultsController * aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@Root];
self.fetchedResultsController = aFetchedResultsController;
fetchedResultsController.delegate = self;

//内存管理。
[aFetchedResultsController release];
[fetchRequest release];
[authorDescriptor release];
[sortDescriptors release];

return fetchedResultsController;
}

显然这是错误的,因为它不是:



a)查看合同实体
b)以任何方式,形状或形式使用公司实体



我知道我需要使用NSPredicate,但我只知道如何说找到所有合同长度大于0并且与公司A合作的员工,然后按下降的人的名字排序,或者甚至按照合同长度。



任何指针或帮助将是伟大的。谢谢。






编辑:首次尝试>

编辑:无法取回合同信息?



我已经能够获得所有在公司工作的员工A回到我的表视图控制器。



但是,我想在我的单元格中显示有关员工及其合同长度/工资的信息。



我试过以下:

  Employee * emp = [fetchedResultsController objectAtIndexPath:indexPath]; 


NSString * firstname = emp.firstname;
NSString * surname = emp.surname;

NSString * fullname = [firstname stringByAppendingString:@];
fullname = [fullname stringByAppendingString:surname];

//记录测试
NSLog(@Name:%@,fullname); //这很好
NSLog(@Contracts:%@,emp.empContracts); // This tell me of a problem withRelationship fault for< NSRelationshipDescription:0x602fdd0>

我相信我需要获得NSPredicate来获取所有的合同数据,而不仅仅是合同数据;

解决方案



/ div>

我认为如果您使用ANY关键字,您会将方程的左边还原为单个数字,这将与等式右边一致:

  NSPredicate * predicate = [NSPredicate predicateWithFormat:@ANY contracts.employer ==%@,employer] 


This is a follow up question to my previous question on:

Core data: Managing employee contracts in a many-to-many relationship?

There is a diagram on that question, and as a quick reminder there is the following:

company --< contracts >-- employees

I have been able to manually save 1 entity inside each of the entities, and verified them all in NSLog.

I've created a CompanyListPage which lists all companies. The idea is that when you click on a company you will be presented with a list of all employees who have a contract with said company.

As context see below:

Company: 
name: A

Contract:
length: 33 wks
salary: 20000

Employee:
name: Bob Jones

In my didSelectRowAtIndex page within my CompanyListPage I have the following.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    Company *c = [fetchedResultsController objectAtIndexPath:indexPath];    
    NSLog(@"You clicked %@", c.name);
    NSString *companyName = c.name;

    EmployeesListPage *employeesListPage = [[EmployeesListPage alloc] initWithNibName:@"EmployeesListPage" bundle:[NSBundle mainBundle]];

    [self.navigationController pushViewController:employeesListPage animated:YES];

    employeesListPage.title = companyName;  
    employeesListPage.managedObjectContext = self.context;
    employeesListPage.managedObject = c;

    [superstarsList release];

}

The problem however is, I am not sure what my NSPredicate should look like when I eventually go to the employeesListPage.

At the moment, its this:

- (NSFetchedResultsController *)fetchedResultsController 
{

    if (fetchedResultsController != nil) {
        return fetchedResultsController;
    }

    // Create and configure a fetch request
    NSFetchRequest *fetchRequest    = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity     = [NSEntityDescription entityForName:@"Employees" inManagedObjectContext:managedObjectContext];
    [fetchRequest setEntity:entity];

    // Create the sort descriptors array
    NSSortDescriptor *authorDescriptor  = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:authorDescriptor, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    // Create and initialize the fetch results controller
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:nil cacheName:@"Root"];
    self.fetchedResultsController = aFetchedResultsController;
    fetchedResultsController.delegate = self;

    // Memory management.
    [aFetchedResultsController release];
    [fetchRequest release];
    [authorDescriptor release];
    [sortDescriptors release];

    return fetchedResultsController;
}    

Obviously this is wrong, because its not:

a) Looking in the contracts entity b) Using the company entity in any way, shape or form

I know I need to use a NSPredicate, but I just know how to make it say "Find me all the employees with a contract length > 0 and working with company A" then order it by the name of the person descending, or even order it by the least contract length first.

Any pointers or help on this would be great. Thank you.


EDIT: First attempt (removed because I got it to work following an answer provided below)

EDIT: Unable to get contract information back?

I've been able to get all the employees that work for Company A back in my table view controller.

However I'm wanting to display in my cell information about the employee and their contract length/salary.

I've tried the following:

Employee *emp = [fetchedResultsController objectAtIndexPath:indexPath];


NSString *firstname = emp.firstname;
NSString *surname = emp.surname;

NSString *fullname = [firstname stringByAppendingString:@" "];
fullname = [fullname stringByAppendingString:surname];

// Logging tests
NSLog(@"Name: %@", fullname); // This is fine
NSLog(@"Contracts: %@", emp.empContracts);  // This tells me of a problem with "Relationship fault for <NSRelationshipDescription: 0x602fdd0>"

I believe I need to get the NSPredicate to grab all the contract data, and not just the contract data; however I may be mistaken.

Again, help on this would be greatly appreciated.

解决方案

I think if you use the ANY keyword, you'll restore the left side of the equation to a single quantity, which will agree with the right side of the equation:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"ANY contracts.employer ==  %@", employer];

这篇关于核心数据 - 从ManyToMany关系构建NSPredicate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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