• 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: Observing Undo in a Core Data App
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Observing Undo in a Core Data App


  • Subject: Re: Observing Undo in a Core Data App
  • From: Ron Lue-Sang <email@hidden>
  • Date: Fri, 24 Feb 2006 16:08:18 -0800

Yeah, what Joshua is suggesting is a much simpler and perfectly reasonable solution.
You could go out of your way to do all the work yourself.
Assuming your array is called "things"


- (void)objectsChangedInContext:(NSNotification *)info {
NSUndoManager *undoManager = [[self managedObjectContext] undoManager];
BOOL work = [undoManager isUndoing] | [undoManager isRedoing];


// only need this stuff in the case your undoing/redoing. You might also need this logic if something other than the array controller will change things in the context
if (work == YES) {


NSSet *deleted = nil;
if ((deleted = [[info userInfo] objectForKey:NSDeletedObjectsKey]) != nil) {
// lazy, we'll skip computing the indexes that will be deleted, but it's a good idea to do
[self willChangeValueForKey:@"things"];
[things removeObjectsInArray:[deleted allObjects]];
[self didChangeValueForKey:@"things"];
}
NSSet *inserted = nil;
if ((inserted = [[info userInfo] objectForKey:NSInsertedObjectsKey]) != nil) {
int baseIndex = [things count];
int endIndex = [inserted count]-1;


NSIndexSet *indexes = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(baseIndex,endIndex)];

// it's easy to compute the indexes here, so we should
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"things"];
[things addObjectsFromArray:[inserted allObjects]];
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexes forKey:@"things"];
}
}
}


- (void)applicationDidFinishLaunching:(NSNotification *)notification {

// register for a notification when objects are changed in the MOC
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(objectsChangedInContext:) name:NSManagedObjectContextObjectsDidChangeNotification object:[self managedObjectContext]];


// fetch and stuff
NSFetchRequest *request = [[[NSFetchRequest alloc] init] autorelease];
[request setEntity:[NSEntityDescription entityForName:@"Entity" inManagedObjectContext:[self managedObjectContext]]];
[self willChangeValueForKey:@"things"];
things = [[[self managedObjectContext] executeFetchRequest:request error:nil] mutableCopy];
[self didChangeValueForKey:@"things"];


}

Make sense?

-----------------------------
Ronzilla
Core Bindings/Cocoa Builder/Interface Data



On Feb 24, 2006, at 11:43 AM, email@hidden wrote:

Message: 12
Date: Fri, 24 Feb 2006 11:19:53 -0600
From: Joshua Scott Emmons <email@hidden>
Subject: Re: Observing Undo in a Core Data App
To: Michael Clark <email@hidden>
Cc: Cocoa List <email@hidden>
Message-ID: <email@hidden>
Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed

What do I observe to see if an item has been placed back into the
NSManagedObject context via Undo. Undo works: the most recently
deleted object is put back into the context - it's there when I
restart the application and reload the array. I just need to know
when an object is put back into the context so that I can update my
UI on the fly. Any suggestions on how to do this?

My solution to this was to crate a base managed object, "Office". Office has a to-many relationship called "employees". My array controller adds and deletes Employee managed objects from this to- many relationship. Then, to recognize undo/redos, I just KVO Office's "employees" relationship.

This was a quick fix when I did it. I didn't look for a better way,
and there very well might be one. But this worked well enough for me.

Oh, one more note. When proceessing the KVO with -
observeValueForKeyPath:ofObject:change:context:,
NSKeyValueChangeKindKey is NSKeyValueChangeRemoval on deletions and
NSKeyValueChangeInsertion on insertions. But as I recall it's NEITHER
on undos and redos. Just a heads up.


Cheers, -Joshua Emmons


_______________________________________________ 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: Objective-C frameworks
  • Next by Date: Re: Error with proxy objects and to-many relationships with Core Data
  • Previous by thread: Re: Observing Undo in a Core Data App
  • Next by thread: String causes NSLog to crash
  • Index(es):
    • Date
    • Thread