CoreData基础 - 多对数关系数组数据 [英] CoreData basics – to-many relationship array data

查看:145
本文介绍了CoreData基础 - 多对数关系数组数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我对CoreData相当新,并且来自MySQL-DB背景,CoreData Moddeling在某种程度上很难理解。我相信你可以帮助我解决这个基本问题。

As I am fairly new to CoreData and coming from a MySQL-DB background, the CoreData Moddeling is kind of hard to understand at some point. I am sure you can help me out with this basic question.

CoreData模型描述符:
我的数据库模型基本上由两个实体组成。第一个称为Manager,第二个称为Zipcodes。 经理有3个属性,目前可以忽略不计。在我看来,重要的是这里的关系称为邮政编码。 Zipcodes - 实体有一个称为zip的属性,它是一个16 int。它也有一个关系,称为经理。

CoreData model-descripton: My database-model basically consists of two entities. The first one is called "Manager", the second one is called "Zipcodes". The "Manager" has 3 attributes, which are negligible at the moment. The important thing in my opinion is here the relationship called "zipcodes". The "Zipcodes"-Entity has an attribute called zip, which is a 16 int. It has a relationship as well, called "manager".

没有我会明白:每个经理都有多个zicode,他负责所有销售。现在的问题是,我已经设置了一个经理实体,并想要链接多个ziplcode给他。每个管理器的zipcodes分隔在一个逗号分隔的字符串中。 (12345,56789,...)

No I'll get to the point: Each manager has multiple zicodes in which he is responsible for all sales. The problem is now that I've setup an manager entity and want to link multiple ziplcodes to him. The zipcodes per manager are seperated in one comma seperated string. (12345,56789,...)

首先我创建一个经理实体。

First of all I am creating an Manager Entity.

Manager *manager = [NSEntityDescription insertNewObjectForEntityForName:@"Manager" inManagedObjectContext:self.managedObjectContext];

下一步是将所有zicode分成一个数组。

The next step is seperating all zicodes to an array.

Manager *manager = [NSEntityDescription insertNewObjectForEntityForName:@"Manager" inManagedObjectContext:self.managedObjectContext];

NSArray *zipcodesArray = [[dict objectForKey:@"zipcodes"] componentsSeparatedByString:@","];

for (NSString *zip in zipcodesArray) {
     ???
}

现在这就是我被困的地方。后来我必须通过searchBar检查zipcodes,他们应该在数据库中分离。我现在必须为每个邮政编码创建一个managedObjectModel吗?如何将所有这些与一个经理实体连接?我确定有办法实现,但我不知道如何。

So now that's the point where I am stuck. As later on I have to check the zipcodes via a searchBar they should be separated in the database. Do I now have to create a managedObjectModel for each zipcode? How do I connect all of them with the "one" manager entity? I'am sure there is a way to achieve that but I don't really know how.

希望我的问题是可以理解的。

Hopefully my question is understandable. If there's anything you would like to know, feel free to ask.

推荐答案

方法

for (NSString *zip in zipcodesArray) {
  NSManagedObject* zipcode = [NSEntityDescription insertNewObjectForEntityName:@"Zipcode" 
                                                  inManagedObjectContext:self.managedObjectContext];
  [zipcode setValue:zip forKey:@"zip"];
  [zipcode setValue:manager forKey:@"manager"];
}

通过建立从邮政编码到最后一行经理的关系,Core数据将自动处理将zipcode插入到从管理员返回邮政编码的关系中。

By establishing the relation from the zipcode to the manager on the last line, Core Data will automatically take care of inserting the zipcode into the relation from the manager back to the zipcodes.

这篇关于CoreData基础 - 多对数关系数组数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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