简体   繁体   English

在CoreData中链接两个对象

[英]link two objects in CoreData

i am new in core data and i created 2 tables,Night and Session. 我是核心数据的新手,我创建了2个表,Night和Session。 i manage to create new object of Night and new object for Session. 我设法创建Night的新对象和Session的新对象。 when i try this code: 当我尝试此代码时:

        Session * session = [NSEntityDescription insertNewObjectForEntityForName:@"Session" inManagedObjectContext:[[DataManager sharedManager] managedObjectContext]];
        Night * night = [NSEntityDescription insertNewObjectForEntityForName:@"Night" inManagedObjectContext:[[DataManager sharedManager] managedObjectContext]];
        night.sessions = [NSSet setWithObject:session];

在此处输入图片说明

the session is getting into the night and the cool thing is, when i Fetch this night and can get the session for the night using: 会话进入深夜,最酷的事情是,当我今晚取回并可以使用以下命令获取夜晚的会话时:

currentNight.Seesion

But i can't see this link in the DB tables :( 但是我在数据库表中看不到这个链接:(

UPDATE: 更新:

I mean when i write night.sessions = [NSSet setWithObject:session]; 我的意思是当我写night.sessions = [NSSet setWithObject:session]; i need to see in the table DB (yes in the DB.sqlite file). 我需要在表DB中查看(在DB.sqlite文件中为是)。

i thought that i should see some thing there ... 我以为我应该在那里看一些东西...

Core Data is not a relational Database .It makes structure of their own.It defines the Database tables structure according to your Managed Objects.For debugging you can see what queries core data is firing on sqlite.This will show you how core data is getting data from these two tables. 核心数据不是relational Database ,它具有自己的结构,它根据您的托管对象定义数据库表的结构。进行调试时,您可以查看核心数据在sqlite上触发了哪些查询,这将向您展示核心数据如何获取这两个表中的数据。

You have to go Product -> Edit Scheme -> Then from the left panel select Run yourApp.app and go to the main panel's Arguments Tab. 您必须转到yourApp.app > Edit yourApp.app >然后从左侧面板中选择Run yourApp.app并转到主面板的Arguments选项卡。

There you can add an Argument Passed On Launch . 在那里,您可以添加“ Argument Passed On LaunchArgument Passed On Launch

You should add -com.apple.CoreData.SQLDebug 1 您应该添加-com.apple.CoreData.SQLDebug 1

Press OK and your are all set. OK ,一切OK

Than next time it will show all the queries it running to fetch data from your tables. 下次,它将显示它正在运行的所有查询以从表中获取数据。

It's not clear to me what your question is. 我不清楚您的问题是什么。 But: 但:

A context is a scratchpad. 上下文是暂存器。 Its contents will not be moved to the persistent store until you -save: . 直到它的内容将不会被移动到持久存储-save: If you drop into the filing system and inspect your persistent store outside of your app without having saved, your changes will not be recorded there. 如果您进入归档系统并在未保存的情况下在应用程序外部检查持久性存储,则所做的更改将不会记录在其中。

For all of the stores the on-disk format is undefined and implementation dependent. 对于所有存储,磁盘格式都是未定义的,并且取决于实现。 So inspecting them outside of Core Data is not intended to show any specific result. 因此,在Core Data之外检查它们并不是要显示任何特定结果。

Anecdotally, if you're using a SQLite store then you should look for a column called Z_SESSIONS or something similar. 有趣的是,如果您使用的是SQLite存储,则应查找名为Z_SESSIONS或类似名称的列。 It'll be a multivalued column. 这将是一个多值列。 Within it will be the row IDs of all linked sessions. 在其中将是所有链接的会话的行ID。 Core Data stores relationships with appropriately named columns and direct row IDs, which are something SQLite supplies implicitly. 核心数据存储具有适当命名的列和直接行ID的关系,这是SQLite隐式提供的。 It does not use an explicit foreign/primary key relationship. 它不使用显式的外键/主键关系。

To emphasise the point: that's an implementation-specific of Core Data. 要强调一点:这是特定于实现的Core Data。 It's not defined to be any more reliable than exactly what ARM assembly LLVM will spit out for a particular code structure. 并没有定义它比确切的ARM程序集LLVM为特定的代码结构要可靠得多。 It's as helpful to have a sense of it as to know about how the CPU tends to cache, to branch predict, etc, but you shouldn't expect to be able to take the SQLite file and use it elsewhere, or in any way interact with it other than via Core Data. 了解它以及了解CPU如何趋向高速缓存,进行分支预测等很有用,但是您不应期望能够获取SQLite文件并在其他地方使用它,或以任何方式进行交互而不是通过核心数据。

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

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