Re: Programmatic removal of an object..
Re: Programmatic removal of an object..
- Subject: Re: Programmatic removal of an object..
- From: Ron Lue-Sang <email@hidden>
- Date: Wed, 13 Aug 2008 13:31:47 -0700
On Aug 12, 2008, at 10:40 PM, R T wrote:
Using Core Data, my xcdatamodel has 1 entity (NSManagedObject) with
10 attributes. I need to add and remove the objects programmatically
(not by buttons). I have...
IBOutlet NSArrayController *ParamsNResults;
IBOutlet NSTableView *ParamsNResultsTableView;
...and have created an object in the tableview
with...
[ParamsNResults newObject];
...but I can not remove it.
my questions:
1. Have I created the object correctly/completely?
2. How do I remove the object?
3. Can I remove all the objects at once?
Thanks
Rick Tschudin
email@hidden
been able to put objects into
Removing all the objects at once is the first thing I need to do
1) If your arraycontroller is set to use your entity and has a
managedObjectContext, [arrayController newObject] will create a
managed object for you properly. The new object will be inserted into
the controller's managedObjectContext.
If your arraycontroller also has "automatically prepares content" set
to YES, then after the object is inserted into the context, the
arraycontroller will get the managedObjectContext's notification that
a new object is available. Then the object will be pulled into the
arraycontroller. Note that this is what would happen even if you
simply created a new managedObject of the same entity using the
designated initializer or NSEnityDescription class method.
So the short answer is: Probably, yes.
2) If you really want to remove the objects programmatically, you
should probably just do it from the managed object context level.
[managedObjectContext deleteObject:theObject]
Again, if you turned on "automatically prepares content" for the
arraycontroller, the managedObjectContext notification that is issued
as a result of the delete (and of the following processPendingChanges)
is enough for the object to disappear from the arrayController's
content.
You should also be able to delete the object from the
arraycontroller's removeObject: or removeObjectAtArrangedObjectIndex:
method. You probably don't want programmatically call the remove:
method which is intended to be invoked as an IBAction - i.e. from a
button in the UI. If you invoke removeObject: and then check to see if
the object is gone, you'll be surprised that nothing's changed. The
add: and remove: methods do their work in the next run through the
event loop.
3) If you're really working programmatically, I'd suggest just looping
over the objects and calling
[managedObjectContext deleteObject:]
--------------------------
RONZILLA
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden