Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Core Data Relationships

Core Data Relationships
Thread Tools
Fresh-Faced Recruit
Join Date: Apr 2007
Status: Offline
Reply With Quote
Apr 5, 2007, 05:01 AM
 
Hello everyone,

I have discovered the awesome power of the Core Data system. I have
one question though. I have set up relationships (which work just
fine) and crated an application. The application has a list on the
left with projects and a tab view on the right. In the tab view, you
can switch to different views to add versions and bugs. I have just
been adding data to the versions and bugs though a table view, but
want to have a window pop up and enter the values. I have no problem
with popping up the window, etc, but I am stuck on saving the data..
This is my current code:

NSManagedObjectContext * context = [[NSApp delegate]
managedObjectContext];
NSManagedObject * version = nil;

version = [NSEntityDescription
insertNewObjectForEntityForName:@"BCVersions"
inManagedObjectContext:context];

[version setValue:[newVersion stringValue]
forKeyPath:@"product_version"];
My only problem is that it is not saving to the relationship it has
with BCProducts. I thought this would happen automatically, but I
guess it doesn't? The data does add, but without the relationship of
corse. I have read several documents from apple about Core Data and
can't find my answer. I bet it is staring at me somewhere close, but
I don't see it...

Anyway, I appreciate any help going in the right direction!

Thanks,

Matt D
     
Fresh-Faced Recruit
Join Date: Sep 2002
Location: California
Status: Offline
Reply With Quote
Apr 12, 2007, 01:35 AM
 
The code you have will add the new 'BCVersions' entity into the managed object context, but it won't set up any of the relationships for you. You need to get at the NSManagedObject representing your selected 'BCProduct'. Then call setValue:forKey: on it with your new 'BCVersions' object.

Here 'selectedProduct' is the NSManagedObject for the 'BCProduct' that you're editing:
[selectedProduct setValue:version forKey:@"version"];
I'd recommend trying to work through a good Core Data tutorial to give you a better understanding of how the relationships work and how to best design your managed object model. Core Data is very powerful, so don't give up on it.
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 02:49 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2