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

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

问题描述

有没有一个教程或源代码显示一个一对多的关系被使用,用户在飞行中添加元素?

解决方案

人们常常被许多人困惑关系,因为一个实体将关系表示为集合,而另一个实体将其表示为单个对象。采取以下实体:

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

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

在EntityA中,关系 bees EntityB对象在关系中。因此,使用Key-Value编码,您必须使用 mutableSetForKey:访问关系,展开所有内容以查看详细信息:

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

...或更紧凑:

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

但是,如果从EntityB端设置,则只添加一个对象,使用setValueForKey:直接像这样:

  [anEntityBObj setValueForKey:anEntityAObj]; 

这是如果你使用通用的NSManagedObject实例来表示你的实体。如果你创建自定义子类,那么你有属性和方法为你做设置:

  [anEntityAObj addBeesObject:anEntityBObj]; 
anEntityBObj.a = anEntityAObj;请记住,对于托管对象,设置关系的一边定义为互易自动设置另一个对象。侧和删除工作相同的方式。



更新




假设我有两个实体 -
person:withnameattribute -
Times:withtimeattribute - i would
想为每个
名称设置多次,但是如何告诉它name
i要将特定时间
添加到


您不需要创建与属性的关系,在这种情况下 name ,而是使用对象,在这种情况下是 Person 实体/类的实例。每个个人 Person 对象与所有其他 Person 对象完全分开,即使它们在<$ c中具有相同的值$ c> name 属性。



您必须获取对任何特定 Parent 对象的引用。如果你刚刚插入了一个新的 Parent 对象,那么你已经有了它的引用。如果它已经被插入/持久化,那么你创建一个带有返回适当对象的谓词的fetch。一旦你有正确的 Parent 对象,你只需添加时间对象的关系。



因此,如果您的实体看起来像这样的伪代码:

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

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

...你使用通用NSManagedObjects来实例化你的实体,你设置现有的 Parent 对象和新的时间对象之间的关系像这样:

  NSManagedObject * existingParent = // ... fetch的结果
NSManagedObject * newTime = [NSEntityDescription insertNewObjectForEntityForName:@TimeinManagedObjectContext:self.moc];
[newTime setValue:[NSDate date] forKey:@theTime];
[newTime setValue:existingParent forKey:@parent];

请注意,如果您从时间 object's side,你可以使用setValue:ForKey:因为从 Time 对象的角度来看,关系只是一个对象到一个对象。



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


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
}

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"];

...or more compactly:

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

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];

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.

Update

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?

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.

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
}

... 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"];

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.

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

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