|
|
Programmatically add to my CoreData NSArrayController?
|
|
|
|
Forum Regular
Join Date: Aug 2005
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
Professional Poster
Join Date: Oct 1999
Location: :ИOITAↃO⅃
Status:
Offline
|
|
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
|
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Aug 2005
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Aug 2005
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Aug 2005
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Aug 2005
Status:
Offline
|
|
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)addResult id)sender
and
- (IBAction)addSource id)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!
|
|
|
|
|
|
|
|
Forum Regular
Join Date: Aug 2005
Status:
Offline
|
|
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!
|
|
|
|
|
|
|
|
Fresh-Faced Recruit
Join Date: Aug 2006
Status:
Offline
|
|
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 Rules
|
|
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is Off
|
|
|
|
|
|
|
|
|
|
|
|