iPhone核心数据插入新对象 [英] iPhone core data inserting new objects

查看:80
本文介绍了iPhone核心数据插入新对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在阅读核心数据文档,觉得我仍然缺少一些东西。



我不太理解你如何插入对象到另一个对象的关系。



例如我的模型中有以下两个实体

  flightDepartureBoard 
name:
from_airport:
to_airport:
current_flights:(这是飞行细节实体的一对多关系)

flight_details
到达
离开
名称

所以我的数据包含不同离开几个机场的板。每个出发板随后都会包含若干flight_details,其中包含该机场当前到达和离港的信息。



我目前的理解是将特定离境董事会的航班详情,我必须获得board的managedObject,然后为每个飞行创建一个新的管理对象,并根据需要设置其值,然后创建一个NSSet,将飞行管理对象合并,并将depatureboards managedObject current_flights(关系)设置为刚刚创建的NSSet。这是正确的吗?



如果我要添加新条目怎么办?我假设我不需要先取出整个集合。



感谢任何帮助..






虽然我只是意识到我可以设置与flightDetails对象上的当前对象的关系。

解决方案

在数据模型中定义关系时,在包含对象的标题中定义集合。只需使用正常的NSMutableSet方法从集合中添加或删除项目即可。



要添加新的受管实体,您将执行以下操作:

  Task * newTask = [NSEntityDescription insertNewObjectForEntityForName:@TaskinManagedObjectContext:self.managedObjectContext]; 

如果您从Apple获取位置示例,然后从那里开始工作,关系。要让它所有的工作只是改变你的数据模型,它需要是什么,不要把它当作一个关系,只是认为它是A有一个NSMutableSet的B。因为你定义的反向关系作为文档推荐你不需要考虑B有什么A,只是开始思考B,当这是对你重要的对象。



假设你有一个机器对象,它有一个Board对象(并且想象你用一个变量 routingArray ,然后只需根据需要创建Flight对象,并设置如下关系:

  Flight * newFlight = [NSEntityDescription insertNewObjectForEntityForName :@FlightinManagedObjectContext:self.managedObjectContext]; 
[newFlight setRoute:routingArray];
[Airport.Board.flights addObject:newFlight];
/ pre>

当航班被取消时(诅咒你,United!),你可以从那个集合中删除Flight,任何试图访问它的地方看不到,所以你可以高兴地毁了乘客的一天。


I have been reading through the core data documentation and feel I am still missing something.

I do not quite understand how you insert objects into a relationship of another object.

For example the following two Entities are in my model

flightDepartureBoard
 name: 
 from_airport:
 to_airport:
 current_flights: (this is a one to many relationship of flight detail entities)

flight_details
 arrive
 depart
 name

So my data contains a list of different departure boards for a few airports. Each departure board then contains a number of flight_details containing info on the current arrivals and departures for that airport.

My current understanding is to insert the flight details for a specific departure board, I must get the managedObject for the board, then create a new managed object for each flight and set its values as appropriate then create an NSSet conatining the flight managed objects and set the depatureboards managedObject current_flights (the relationship) to the just created NSSet. Is this correct?

What if I want to add new entries? I assume I do not need to fetch the entire set first?

Thanks for any help..


Although I just realised I could set the relationship to the current object on the flightDetails object instead..

解决方案

When you define the relationship in the data model the set is defined in the header of the containing object. Just add or remove items from the set using the normal NSMutableSet methods.

To add a new managed entity you will do something like:

Task* newTask = [NSEntityDescription insertNewObjectForEntityForName:@"Task" inManagedObjectContext:self.managedObjectContext];

If you grab the Location sample from Apple and work from there you will puzzle it out although that contains no relationships. To get it all working just change your data model to what it needs to be and don't think of it as a relationship, just think of it as "A has a NSMutableSet of B". Because you defined the reverse relationship as the documentation recommended you don't need to think about what B has of A, just start thinking in terms of B when that's the object that matters to you.

Let's say you have an Airport object which has a Board object (and imagining you set origin and destination into Flight with a variable routingArray, then just create your Flight objects as necessary and set the relationship like:

Flight* newFlight = [NSEntityDescription insertNewObjectForEntityForName:@"Flight" inManagedObjectContext:self.managedObjectContext];
[newFlight setRoute:routingArray];
[Airport.Board.flights addObject:newFlight];

When the flight is cancelled (curse you, United!) you can just remove the Flight from that set and anyone who tries to access the object where it used to be see nil, so you can cheerfully ruin the passengers day.

这篇关于iPhone核心数据插入新对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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