如何管理和使用“多对多”核心数据关系? [英] How do you manage and use "Many to many" core data relationships?

查看:111
本文介绍了如何管理和使用“多对多”核心数据关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个应用程序,并试图使用核心数据,因为它似乎是目标C批准的方式来创建数据存储系统。用例我涉及多对多的关系,就像你通常在标准的SQL系统中看到的。我理解,目标C不是一个数据库,工作方式不同。我也在这里查看了文档:

I'm creating an app and trying to use core data, because it seems like it's the objective-C approved way to create a data storage system. The use case I have involves "many to many" relationships, like you'd normally see in a standard SQL system. I understand that objective C is not a database and works differently. I've also reviewed the documentation here:

http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/CoreData/Articles/cdRelationships.html# // apple_ref / doc / uid / TP40001857-SW10

还有其他几个地方。然而,我还是有麻烦。有人可以向我解释如果你有一个用例,你需要使用SQL交叉引用表你会做什么?例如:

And several other places. And yet, I'm still having trouble. Can someone explain to me what you would do if you have a use case where you'd need to use a SQL cross reference table? For example:

Managers |员工

Managers | Employees

经理可能有几个员工,但员工也可能有几个经理。在SQL中,我将创建一个交叉引用表,然后使用它。

Manager may have several employees, but the employees might also have several manager. In SQL, I'd create a cross reference table and then use that.

示例: http:// www .tomjewett.com / dbdesign / dbdesign.php?page = manymany.php

有人可以解释如何在Core数据中做到这一点吗?

Can someone explain how you could do that in Core data?

根据核心数据文档,他们说:

According to the core data documentation, they say this:

您定义了一个多对多关系使用两对多关系,第一对多关系从第一个实体到第二个实体,第二个对多关系从第二个实体到第一个实体,然后将每个关系设置为其他。(如果你有数据库管理的背景,这让你担心,不要担心:如果你使用SQLite存储,Core Data会自动为你创建中间连接表。)

"You define a many-to-many relationship using two to-many relationships. The first to-many relationship goes from the first entity to the second entity. The second to-many relationship goes from the second entity to the first entity. You then set each to be the inverse of the other. (If you have a background in database management and this causes you concern, don't worry: if you use a SQLite store, Core Data automatically creates the intermediate join table for you.)"

但是,除了不担心,我不知道这可能如何工作?

But, besides "not being worried," I don't know how this could possibly work?

推荐答案

彼得:我有九个老板,鲍勃。
Bob:再说一遍?
Peter:

把你的大脑从数据库中删除。不要询问如何访问交叉引用表。请询问如何查找经理的员工或员工的经理。

Take your brain out of the database. Do not ask how to access a cross-reference table. Ask how to find the employees for a manager or the managers for an employee.

如果使用NSManagedObject的自定义子类,可以将属性声明为文档。然后你可以简单地说:

If you use a custom subclass of NSManagedObject, you can declare the properties as documented. Then you can simply say:

NSSet *mgrsEmployees = mgr.employees;
NSSet *employeesMgrs = employee.managers;

这不会给你所有的员工和所有经理,该雇员的所有经理。要更改关系:

That will not give you all the employees and all the managers, only all the employees for that manager and all the managers for that employee. To change relationships:

[mgr addEmployeesObject:newEmployee];
[newEmployee addManagersObject:mgr]; // not necessary, automatic if you define inverse

[mgr removeEmployeesObject:transferredEmployee];
[transferredEmployee removeManagersObject:mgr]; // not necessary
[newMgr addEmployeesObject:transferredEmployee];
[transferredEmployee addManagersObject:newMgr]; // not necessary

你只需要做每对语句之一,如果您已将管理员和员工定义为彼此的倒置。

You only need to do either one of each pair of statements, and it will implicitly do the other, provided you have defined managers and employees as inverses of each other.

如果您不使用自定义子类,访问会更加冗长。 p>

If you don't use a custom subclass, accessing is a little more verbose

NSSet *mgrsEmployees = [mgr valueForKey:@"employees"];
NSSet *employeesMgrs = [employee valueForKey:@"managers"];

NSMutableSet *changeMgrsEmployees = [mgr mutableSetValueForKey:@"employees"];
[changeMgrsEmployees addObject:newEmployee];
// not necessary
NSMutableSet *changeEmployeesMgrs = [employee mutableSetValueForKey:@"managers"];
[changeEmployeesMgrs addObject:mgr];

等。

这篇关于如何管理和使用“多对多”核心数据关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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