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 > Programmatically add to my CoreData NSArrayController?

Programmatically add to my CoreData NSArrayController?
Thread Tools
HungrySeacow
Forum Regular
Join Date: Aug 2005
Status: Offline
Reply With Quote
Apr 4, 2006, 04:13 PM
 
OK, I have set everything involving CoreData in IB so far. Now, I have a NSTableView that displays the arrangedObjects of a managedObjectContext. I have a button that connects to the add: action on the NSArrayController that manages the managedObjectContext. I would like to connect that button to a programmatically written statement that does the same thing that the add: function did.

I have looked for some examples online and can't find anything easy enough for me to understand . The documentation on NSObjectController is giving me a headache. I am guessing that I have to acquire the managedObjectContext and then tell it newObject. But how? Thanks!
( Last edited by HungrySeacow; Apr 4, 2006 at 07:56 PM. )
YummySoup! - Recipe management and sharing at its best!
     
Mithras
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status: Offline
Reply With Quote
Apr 4, 2006, 04:43 PM
 
Code:
newFoo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext: [self managedObjectContext]];
This works within the app delegate. Elsewhere, you'll want to call [[NSApp delegate] managedObjectContext] to get the right context.
The wonders of KVC should take care of the notification of your NSArrayController.
See also:
http://developer.apple.com/macosx/coredata.html
     
HungrySeacow  (op)
Forum Regular
Join Date: Aug 2005
Status: Offline
Reply With Quote
Apr 4, 2006, 05:29 PM
 
Originally Posted by Mithras
Code:
newFoo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext: [self managedObjectContext]];
This works within the app delegate. Elsewhere, you'll want to call [[NSApp delegate] managedObjectContext] to get the right context.
The wonders of KVC should take care of the notification of your NSArrayController.
See also:
http://developer.apple.com/macosx/coredata.html
Do I use this to declare newFoo?
Code:
NSEntityDescription *newFoo;
Then:
Code:
newFoo = [NSEntityDescription insertNewObjectForEntityForName:@"Foo" inManagedObjectContext: [self managedObjectContext]];
YummySoup! - Recipe management and sharing at its best!
     
HungrySeacow  (op)
Forum Regular
Join Date: Aug 2005
Status: Offline
Reply With Quote
Apr 4, 2006, 05:53 PM
 
Sorry, never-mind my previous post. I figured out that it needs to be declared as an NSMangedObject. Now I have a different problem. The entity is bound to a contentSet of another entity's selection. So when the new add: method is called nothing happens. However, If I used your code to add to the very first entity's managedContext sure enough it works. I guess what I have to do is add it to both entities... err.....
YummySoup! - Recipe management and sharing at its best!
     
HungrySeacow  (op)
Forum Regular
Join Date: Aug 2005
Status: Offline
Reply With Quote
Apr 4, 2006, 05:58 PM
 
I am guessing that I will need an IBOutlet to the "source" NSTableView so I can access it's selected row or something.
YummySoup! - Recipe management and sharing at its best!
     
HungrySeacow  (op)
Forum Regular
Join Date: Aug 2005
Status: Offline
Reply With Quote
Apr 4, 2006, 11:32 PM
 
OK, I have created a sample program that demonstrates my problem:
http://www.hungryseacow.com/Personal...aTableView.zip

The Manual Add Method button for the Source managedObject works fine, However the one for the Results managedObject does not. Take a look at the methods in the __AppController.m file

- (IBAction)addResultid)sender
and
- (IBAction)addSourceid)sender

Now if you look at the Results NSArrayController, you can see that it has two bindings, the contentSet is bound to the Source's selection. There must be some way to modify this method to get it to also work with the contentSet.

Code:
- (IBAction)addResult:(id)sender { NSLog(@"addResult called"); NSManagedObject *source; source = [NSEntityDescription insertNewObjectForEntityForName:@"Result" inManagedObjectContext: [self managedObjectContext]]; [[self managedObjectContext] processPendingChanges]; }
YummySoup! - Recipe management and sharing at its best!
     
HungrySeacow  (op)
Forum Regular
Join Date: Aug 2005
Status: Offline
Reply With Quote
Apr 5, 2006, 04:41 PM
 
I don't know why I didn't think of this before! I fixed the problem. I created an IBOutlet to the NSArray Controller called resultArrayController and then sent it this message:
Code:
NSManagedObject *source; [resultArrayController add: source];
EDIT: Sorry the solution that I had before was wrong, now it has been corrected.
( Last edited by HungrySeacow; Apr 5, 2006 at 11:24 PM. )
YummySoup! - Recipe management and sharing at its best!
     
MatzeLoCal
Fresh-Faced Recruit
Join Date: Aug 2006
Status: Offline
Reply With Quote
Aug 16, 2006, 10:03 AM
 
May I add a question to this? Yes? Thank you

So, I'm new to CoreData and I'm a bit curious.

I have a Simple DataModell
Just one Entity with the Attributes idUser and userName. This is bound to a NSArrayController. The data is represented in a NSTableView with an add and remove button. Yap, this is looking quite like those examples in CoreData-Tutorials. And that far every thing works out quite well for me, but now I want codewise (custom class, not the AppDelegate) read out the data for the selected in the NSTableView.

How do I do that? I played a bit around, but I guess I tried to much in the wrong direction.

Thank you very much in advance.
MatzeLoCal
     
   
 
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
Top
Privacy Policy
All times are GMT -4. The time now is 07:04 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,