• 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
CoreData and undo/redo
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

CoreData and undo/redo


  • Subject: CoreData and undo/redo
  • From: Kenny Leung <email@hidden>
  • Date: Sat, 2 Sep 2006 10:25:18 -0700

Hi All.

Here is a little info in the hopes that it will save you some time.

It is often the case that you have a quick lookup (by name, for instance) of cached values for a to-many relationship. You implement addSuctionBallsObject: and removeSuctionBallsObject: to add and remove the relationship objects from the cache whenever they added and removed from the relationship.

This all works fine in the regular operation of your application, but breaks down when you do undo/redo. Unfortunately, CoreData does things under the covers with undo/redo, and never calls these methods. So how to handle this? You could observe yourself, but that seems like an inefficient way to do things.

One way that seems to work pretty well is to register undo and redo operations inside your addSuctionBallsObject: and removeSuctionBallsObject: methods. Here is an example of the pattern.

- (void)addSuctionBallsObject:(SuctionBall *)value
{
    [super addSuctionBallsObject:value];
    [self _redoAddSuctionBallsObject:value];
}

- (void)_undoAddSuctionBallsObject:(NSDictionary *)dict
{
[_suctionBallsByName removeObjectForKey:[dict objectForKey:@"suctionBallName"]];
[[self undoManager] registerUndoWithTarget:self selector:@selector(_redoAddSuctionBallsObject:) object:[dict objectForKey:@"suctionBallName"]];
}


- (void)_redoAddSuctionBallsObject:(SuctionBall *)value
{
    NSString *suctionBallName;

suctionBallName = [value name];
[_suctionBallsByName setObject:value forKey:suctionBallName];
[[self undoManager] registerUndoWithTarget:self selector:@selector(_undoAddSuctionBallName:) object:[NSDictionary dictionaryWithObjectsAndKeys:
value, @"suctionBall",
[value name], @"suctionBallName",
nil]];
}



-Kenny

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Prev by Date: Re: Re: NSSlider did finish sliding
  • Next by Date: Re: post http problem in cocoa
  • Previous by thread: Re: Binding different elements of an UI.
  • Next by thread: Question about categories
  • Index(es):
    • Date
    • Thread