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

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

问题描述

我正在创建一个应用程序并尝试使用核心数据,因为它似乎是 Objective-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:

经理 |员工

经理可能有多个员工,但员工也可能有多个经理.在 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 data 中做到这一点吗?

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?

推荐答案

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

Peter: "I have nine bosses, Bob." Bob: "Say again?" Peter: "Nine."

把你的大脑从数据库中拿出来.不要询问如何访问交叉引用表.询问如何为经理查找员工或为员工查找经理.

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.

如果你不使用自定义子类,访问会稍微冗长一些

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天全站免登陆