我在哪里可以找到Core Data to-many关系的一个很好的例子?

时间:2022-02-25 15:08:56

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.

有没有人有一个教程或源代码,显示正在使用的多对多关系,用户在哪里添加元素?我想看一看,因为阅读它并没有多大帮助。

8 个解决方案

#1


23  

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:

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

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:

但是,如果从EntityB端设置,则只添加单个对象,因此您可以直接使用setValueForKey:如下所示:

[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:

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

[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?

让我说我有2个实体 - 人:具有“名称”属性 - 时间:具有“时间”属性 - 我想为每个名称设置多次,但是如何告诉它我想要的名称添加特定时间?

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.

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

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.

您必须获取对任何特定Parent对象的引用。如果您刚刚插入了一个新的Parent对象,那么您已经有了对它的引用。如果已经插入/持久化,则使用谓词创建一个提取,该谓词将返回正确的对象。一旦拥有了正确的Parent对象,就可以将Time对象添加到关系中。

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:

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

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.

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

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.

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

#2


10  

Here is an example of a one-to-many relationship, as long as you have set up the relationships so they have an inverse they are quite easy to manage. at the end, Entity1's chilren attribute will contain entity2 and entity3

以下是一对多关系的示例,只要您设置了关系,以便它们具有逆,它们就很容易管理。最后,Entity1的chilren属性将包含entity2和entity3

Entity1 *entity1=[NSEntityDescription insertNewObjectForEntityForName:@"Entity1" inManagedObjectContext:self.managedObjectContext];
Entity2 *entity2=[NSEntityDescription insertNewObjectForEntityForName:@"Entity2" inManagedObjectContext:self.managedObjectContext];
Entity2 *entity3=[NSEntityDescription insertNewObjectForEntityForName:@"Entity2" inManagedObjectContext:self.managedObjectContext];

[entity2 setParentEntity:entity1];
[entity3 setParentEntity:entity1];
NSError *error;
[[self managedObjectContext]save:&error];

NSSet *set=[entity1 children];

我在哪里可以找到Core Data to-many关系的一个很好的例子?

#3


3  

Get Marcus Zarra's book "Core Data" (you can buy a PDF rather than paper for instant gratification...) It too presents examples in Mac OS X but it goes a lot further than just getting started. You will find out useful things like versioning, transition rules, optimization. This book saved one of my projects from being late & slow!

获取Marcus Zarra的书“核心数据”(你可以购买PDF而不是纸张以获得即时满足感......)它也提供了Mac OS X中的示例,但它比起步更进一步。您将找到有用的东西,如版本控制,转换规则,优化。这本书保存了我的一个项目,从迟到和慢!

#4


3  

I am currently using this ebook and its very well written: It explains and shows how to create datamodels and there are also there are code examples..

我目前正在使用这本电子书并写得很好:它解释并说明了如何创建数据模型,还有代码示例..

"Pro Core Data for iOS" http://apress.com/book/view/1430233559

“适用于iOS的专业核心数据”http://apress.com/book/view/1430233559

Hope it helps

希望能帮助到你

#5


3  

Download the iPhoneCoreDataRecipes sample code. It's a full working demo of Core Data and an excellent way to get familiarized with the concepts.

下载iPhoneCoreDataRecipes示例代码。这是Core Data的完整工作演示,是熟悉这些概念的绝佳方式。

#6


2  

You could try to see in the "Learn Cocoa on the Mac" book sources. In the chapters 7 and 8.

您可以尝试在“学习Mac上的Cocoa”书籍来源中看到。在第7章和第8章中。

I hope you'll find what you need

我希望你能找到你需要的东西

#7


0  

See Apple's Core Data Programming Guide.

请参阅Apple的核心数据编程指南。

I create objects and add them to relationships using mutableSetValueForKey:

我使用mutableSetValueForKey创建对象并将它们添加到关系中:

mutableSetValueForKey: returns a mutable proxy object. If you mutate its contents, it will emit the appropriate key-value observing (KVO) change notifications for the relationship.

mutableSetValueForKey:返回一个可变的代理对象。如果改变其内容,它将为关系发出适当的键值观察(KVO)更改通知。

#8


0  

Ray Wenderlich has great Core Data tutorials.

Ray Wenderlich拥有出色的核心数据教程。

this one deals with to-many relationships

这个涉及到很多关系

which builds off

建立起来的

this great 3-part intro to core data tutorial

这篇精彩的3部分介绍了核心数据教程

#1


23  

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:

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

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:

但是,如果从EntityB端设置,则只添加单个对象,因此您可以直接使用setValueForKey:如下所示:

[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:

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

[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?

让我说我有2个实体 - 人:具有“名称”属性 - 时间:具有“时间”属性 - 我想为每个名称设置多次,但是如何告诉它我想要的名称添加特定时间?

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.

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

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.

您必须获取对任何特定Parent对象的引用。如果您刚刚插入了一个新的Parent对象,那么您已经有了对它的引用。如果已经插入/持久化,则使用谓词创建一个提取,该谓词将返回正确的对象。一旦拥有了正确的Parent对象,就可以将Time对象添加到关系中。

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:

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

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.

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

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.

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

#2


10  

Here is an example of a one-to-many relationship, as long as you have set up the relationships so they have an inverse they are quite easy to manage. at the end, Entity1's chilren attribute will contain entity2 and entity3

以下是一对多关系的示例,只要您设置了关系,以便它们具有逆,它们就很容易管理。最后,Entity1的chilren属性将包含entity2和entity3

Entity1 *entity1=[NSEntityDescription insertNewObjectForEntityForName:@"Entity1" inManagedObjectContext:self.managedObjectContext];
Entity2 *entity2=[NSEntityDescription insertNewObjectForEntityForName:@"Entity2" inManagedObjectContext:self.managedObjectContext];
Entity2 *entity3=[NSEntityDescription insertNewObjectForEntityForName:@"Entity2" inManagedObjectContext:self.managedObjectContext];

[entity2 setParentEntity:entity1];
[entity3 setParentEntity:entity1];
NSError *error;
[[self managedObjectContext]save:&error];

NSSet *set=[entity1 children];

我在哪里可以找到Core Data to-many关系的一个很好的例子?

#3


3  

Get Marcus Zarra's book "Core Data" (you can buy a PDF rather than paper for instant gratification...) It too presents examples in Mac OS X but it goes a lot further than just getting started. You will find out useful things like versioning, transition rules, optimization. This book saved one of my projects from being late & slow!

获取Marcus Zarra的书“核心数据”(你可以购买PDF而不是纸张以获得即时满足感......)它也提供了Mac OS X中的示例,但它比起步更进一步。您将找到有用的东西,如版本控制,转换规则,优化。这本书保存了我的一个项目,从迟到和慢!

#4


3  

I am currently using this ebook and its very well written: It explains and shows how to create datamodels and there are also there are code examples..

我目前正在使用这本电子书并写得很好:它解释并说明了如何创建数据模型,还有代码示例..

"Pro Core Data for iOS" http://apress.com/book/view/1430233559

“适用于iOS的专业核心数据”http://apress.com/book/view/1430233559

Hope it helps

希望能帮助到你

#5


3  

Download the iPhoneCoreDataRecipes sample code. It's a full working demo of Core Data and an excellent way to get familiarized with the concepts.

下载iPhoneCoreDataRecipes示例代码。这是Core Data的完整工作演示,是熟悉这些概念的绝佳方式。

#6


2  

You could try to see in the "Learn Cocoa on the Mac" book sources. In the chapters 7 and 8.

您可以尝试在“学习Mac上的Cocoa”书籍来源中看到。在第7章和第8章中。

I hope you'll find what you need

我希望你能找到你需要的东西

#7


0  

See Apple's Core Data Programming Guide.

请参阅Apple的核心数据编程指南。

I create objects and add them to relationships using mutableSetValueForKey:

我使用mutableSetValueForKey创建对象并将它们添加到关系中:

mutableSetValueForKey: returns a mutable proxy object. If you mutate its contents, it will emit the appropriate key-value observing (KVO) change notifications for the relationship.

mutableSetValueForKey:返回一个可变的代理对象。如果改变其内容,它将为关系发出适当的键值观察(KVO)更改通知。

#8


0  

Ray Wenderlich has great Core Data tutorials.

Ray Wenderlich拥有出色的核心数据教程。

this one deals with to-many relationships

这个涉及到很多关系

which builds off

建立起来的

this great 3-part intro to core data tutorial

这篇精彩的3部分介绍了核心数据教程