• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: core data - delete of object.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: core data - delete of object.


  • Subject: Re: core data - delete of object.
  • From: Ashley Clark <email@hidden>
  • Date: Tue, 18 Nov 2008 15:39:40 -0600

That's true, processPendingChanges is used by the context to implement undo functionality but there's no reason why you can't call it yourself after your delete since you've disabled the context's undo functionality.


Here's an example where events is an NSArrayController:

- (IBAction)deleteEvent:(id)sender {
    NSManagedObjectContext *context = [self managedObjectContext];

if ([events selectedObjects] && [[events selectedObjects] count]) {
[context deleteObject:[[events selectedObjects] objectAtIndex:0]];
[context processPendingChanges];
}
}


Or add a category to NSManagedObjectContext:

- (void)AC_deleteObjectAndProcessPendingChanges:(id)object {
	[self deleteObject:object];
	[self processPendingChanges];
}


Why disable the undo functionality at the context level though? If you're providing your own undo support elsewhere there's nothing that I know of that says you have to use the context's undoManager elsewhere. If you have sections of code that you don't want to be undoable you can still call disableUndoRegistration and enableUndoRegistration on the context's undoManager itself too (as long as you don't want any objectsDidChange notifications during that block).

Ashley


On Nov 18, 2008, at 6:44 AM, John Clayton wrote:

I guess processPendingChanges is only called if there is an undoManager? Correct? I don't have one - I disabled it intentionally.

I've tried that notification, its only fired on save of the context.

On 18/11/2008, at 1:22 PM, Ashley Clark wrote:

According to the documentation and to my quick test, the NSManagedObjectContextObjectsDidChangeNotification is sent during processPendingChanges. At the very least that happens at the end of every event loop automatically. Even if you've bypassed your event loop somehow you can still call processPendingChanges on your context yourself after your object deletion and have it do whatever extra processing you need.

ie.

NSManagedObjectContext *context = [theObject managedObjectContext];
[context deleteObject:theObject];
[context processPendingChanges];


Ashley


On Nov 18, 2008, at 12:54 AM, John Clayton wrote:

Thanks Jim, unfortunately all occur either at pre-save or save time, which is too late.

I'm basically looking to restore some state on an object at time of deletion, e.g.

if A is associated with B via a to-many relationship from A->B, then when one instance of B is deleted - we need to change a property on A.


On 17/11/2008, at 10:59 PM, Jim Correia wrote:

On Nov 17, 2008, at 4:51 PM, John Clayton wrote:

I'm using core-data and need to know when a particular core-data object (derived from NSManagedObject of course) is about to be deleted. I'm deleting objects simply by using the managed object context's deleteObject method, like this:
[[theObject managedObjectContext] deleteObject:theObject];


Is there a way to be notified immediately of the delete? I need to know immediately, not only when the context is saved. Am I missing something painfully obvious here?

now, I could of course put a 'deleteThisObject' method onto the class I'm deleting, but isn't there a core-data way to handle this?

I've tried catching the NSManagedObjectContextObjectsDidChangeNotification notification - but that only fires on context saves, same with the willTurnIntoFault method as well as the dealloc - all these methods are only ever called when the context is being saved - which in my case is (a) unpredictable, (b) too late .

Is -validateForDelete: time too late? If not, that's an option.

You can also try

[context deleteObject: object];
[context processPendingChanges];

... handle notification ...

[context save: &error];




_______________________________________________

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


References: 
 >core data - delete of object. (From: John Clayton <email@hidden>)
 >Re: core data - delete of object. (From: Jim Correia <email@hidden>)
 >Re: core data - delete of object. (From: John Clayton <email@hidden>)
 >Re: core data - delete of object. (From: Ashley Clark <email@hidden>)
 >Re: core data - delete of object. (From: John Clayton <email@hidden>)

  • Prev by Date: Potentially dumb question
  • Next by Date: Re: Potentially dumb question
  • Previous by thread: Re: core data - delete of object.
  • Next by thread: OutlineView, bindings and array of NSDictionaries.
  • Index(es):
    • Date
    • Thread