简体   繁体   English

插入对象-核心数据

[英]Inserting objects — Core Data

I have just started using Core Data and I am now having trouble understanding how to insert objects. 我刚刚开始使用Core Data,但现在在理解如何插入对象方面遇到了麻烦。 I have the following structure: 我有以下结构:

  • Location 位置
  • Environment 环境
  • Log 日志记录

Log, which is the main "row" has a many-to-many relationship to both Environment and Location. 日志是主要的“行”,与环境和位置都具有多对多的关系。 And those entities also has a many-to-many relationship to the Log entity. 这些实体也与Log实体具有多对多关系。

Both Location and Environment only has one simple attribute. 位置和环境都只有一个简单的属性。 The Log entity stores time, date and some other data. 日志实体存储时间,日期和其他一些数据。

However I do not understand how do insert process work? 但是我不明白插入过程如何工作? I want to make a Log object, that sets the data already mentioned as-well as the locations and the environments. 我要创建一个Log对象,该对象设置已经提到的数据以及位置和环境。 How should I do this? 我应该怎么做?

And I also wonder how to Core Data stores the Location and Environment and links it to a Log. 而且我还想知道如何核心数据存储位置和环境并将其链接到日志。

Because both the location and environment entity will only have maximum four objects in it. 因为位置实体和环境实体中最多只能包含四个对象。 So I suppose that the reason why I use NSSet is because the data wont be duplicated? 所以我想使用NSSet的原因是因为数据不会被复制? For in SQL these two tables would have been filled with the four possible values (Location): "temp1", "temp2", "temp3", "temp4" and those would have a foreign key in the mapping table so that the values would not be duplicated. 因为在SQL中,这两个表将用四个可能的值(位置)填充:“ temp1”,“ temp2”,“ temp3”,“ temp4”,并且它们在映射表中将具有外键,以便这些值不可重复。

If that makes any sense I would appreciate help from you guys. 如果有任何意义,我将感谢您的帮助。 Thank you for your time. 感谢您的时间。

As has been written many times before, Core Data is not the same as SQL. 如前所述,Core Data与SQL不同。 Core Data is a much richer object model API that happens, in some cases, to use SQLite as a backing store. 核心数据是一个更丰富的对象模型API,在某些情况下,它使用SQLite作为后备存储。

That said, you should take a look at some of Apple's Sample Code for examples of Core Data programming. 也就是说,您应该看一下Apple的一些示例代码 ,以获取Core Data编程示例。 You will see the mantra that is necessary to setup various Core Data instances (eg, NSPersistentStoreCoordinator, NSManagedObjectContext). 您将看到设置各种Core Data实例(例如,NSPersistentStoreCoordinator,NSManagedObjectContext)所必需的咒语。 And you will also see how new NSManagedObject instances are created. 您还将看到如何创建新的NSManagedObject实例。

You might also look at the Core Data Programming Guide , if you haven't already. 如果您还没有的话,也可以参阅《 核心数据编程指南》

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM