简体   繁体   中英

link two objects in CoreData

i am new in core data and i created 2 tables,Night and Session. i manage to create new object of Night and new object for 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]; i need to see in the table DB (yes in the DB.sqlite file).

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.

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.

There you can add an Argument Passed On Launch .

You should add -com.apple.CoreData.SQLDebug 1

Press OK and your are all set.

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

Anecdotally, if you're using a SQLite store then you should look for a column called Z_SESSIONS or something similar. It'll be a multivalued column. Within it will be the row IDs of all linked sessions. Core Data stores relationships with appropriately named columns and direct row IDs, which are something SQLite supplies implicitly. It does not use an explicit foreign/primary key relationship.

To emphasise the point: that's an implementation-specific of 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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