我在哪里可以找到 Core Data 对多关系的好例子? [英] Where can I find a good example of a Core Data to-many relationship?

查看:15
本文介绍了我在哪里可以找到 Core Data 对多关系的好例子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有人有教程或源代码显示正在使用的多对多关系,用户可以在其中动态添加元素?我只是想看看,因为阅读它并没有太大帮助.

Does anyone have a tutorial or source code that shows a to-many relationship being used, where the user add elements in on the fly? I would just like to have a look because reading about it hasnt been much help.

推荐答案

人们经常对多对多关系感到困惑,因为一个实体将关系表示为一组,而另一个将其表示为单个对象.取以下实体:

People are often confused by to-many relationships because one entity represents the relationship as a set while the other represents it as a single object. Take the following entities:

EntityA{
    name:string
    bees<-->>EntityB.a
}

EntityB{
    name:string
    a<<-->EntityA.bees
}

在EntityA 中,关系bees 是一个集合,因为关系中可能有很多EntityB 对象.因此,使用键值编码,您必须使用 mutableSetForKey: 来访问关系,将所有内容展开以查看详细信息,如下所示:

In EntityA, the relationship bees is a set because there maybe many EntityB objects in the relationship. So, using Key-Value coding, you would have to access the relationship using a mutableSetForKey: expanding everything out to see the detail would like so:

NSMutableSet *muteSet=[anEntityAObj mutableSetValueForKey:@"bees"];
[muteSet addObject:aNewBObj];
[anEntityAObj setValueForKey:@"bees"];

...或更紧凑:

[[anEntityAObj mutableSetValueForKey:@"bees"] addObject:aNewBObj];

但是,如果您从 EntityB 端进行设置,则您只添加了一个对象,因此您可以直接使用 setValueForKey: 像这样:

If you set from the EntityB side, however, you are only adding a single object so you can just use setValueForKey: directly like so:

[anEntityBObj setValueForKey:anEntityAObj];

如果你使用通用的 NSManagedObject 实例来表示你的实体.如果您创建自定义子类,那么您就有属性和方法来为您进行设置:

That's if you use generic NSManagedObject instances to represent your entities. If you create custom subclasses then you have properties and methods to do the setting for you:

[anEntityAObj addBeesObject:anEntityBObj];
anEntityBObj.a=anEntityAObj;

还请记住,对于托管对象,将定义为互惠的关系的一侧设置为自动设置另一侧,并以相同的方式删除.

Remember as well that with managed objects, setting one side of a relationship defined as reciprocal automatically set the other side as well and removing works the same way.

假设我有 2 个实体——人员:具有名称"属性-时间:带有时间"属性——我愿意想为每个设置多次名字,但我怎么告诉它哪个名字我想添加具体时间到?

Lets say i've got 2 entities -- Person:with "name" attribute -- Times:with "time" attribute -- i would want to set multiple times for each name, but how to I tell it which name i would like to add the specific times to?

您不与属性(在本例中为 name)创建关系,而是与对象(在本例中为 Person 实体/类的实例)创建关系.每个单独的 Person 对象都与所有其他 Person 对象完全分开,即使它们的 name 属性具有相同的值.

You don't create relationships with attributes, in this case name but rather with an object, in this case an instances of the Person entity/class. Each individual Person object is completely separate from all other Person objects even if they have the same value in their name attribute.

您必须获得对任何特定 Parent 对象的引用.如果您刚刚插入了一个新的 Parent 对象,那么您已经有了对它的引用.如果它已经被插入/持久化,那么你创建一个带有谓词的提取,它将返回正确的对象.一旦您拥有正确的 Parent 对象,您只需将 Time 对象添加到关系中即可.

You must obtain a reference to any particular Parent object. If you have just inserted a new Parent object then you already have a reference to it. If it is already been inserted/persisted, then you create a fetch with a predicate that will return the proper object. Once you have the correct Parent object you then just add the Time objects to the relationship.

所以,如果你的实体看起来像这个伪代码:

So, if your entity looks like this pseudo-code:

Parent{
    name:string
    times<-->>Time.parent
}

Time{
    theTime:date
    parent<<-->Parent.times
}

...并且您正在使用通用 NSManagedObjects 来实例化您的实体,您将现有的 Parent 对象和新的 Time 对象之间的关系设置为如下所示:

... and you are using generic NSManagedObjects to instatiate you entities, you set the relationship between an existing Parent object and new Time object something like this:

NSManagedObject *existingParent= //... results of a fetch
    NSManagedObject *newTime=[NSEntityDescription insertNewObjectForEntityForName:@"Time" inManagedObjectContext:self.moc];
    [newTime setValue:[NSDate date] forKey:@"theTime"];
    [newTime setValue:existingParent forKey:@"parent"];

请注意,如果您从 Time 对象一侧设置关系,则可以使用 setValue:ForKey: 因为从 Time 对象的角度来看,该关系只是一个对象到一个对象.

Note that if you set the relationship from the Time object's side, you can use setValue:ForKey: because from the Time object's perspective the relationship is just one object to one object.

一旦你开始考虑对象而不是数据库,这真的很简单.您在上下文中插入的每个对象都是唯一的,即使它与同一实体/类的其他对象共享属性.这就是为什么您可以在特定对象之间设置关系,而不必担心存储在其属性中的值.

It is really quite simple once you start thinking in objects instead of databases. Each object you insert in a context is unique even if it shares attributes with other objects of the same entity/class. That is why you can set a relationship between specific objects without necessarily worrying about the values stored in their attributes.

这篇关于我在哪里可以找到 Core Data 对多关系的好例子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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