Re: Strange Managed Context Error
Re: Strange Managed Context Error
- Subject: Re: Strange Managed Context Error
- From: Colin Cornaby <email@hidden>
- Date: Sun, 21 Aug 2005 14:29:56 -0700
On Aug 21, 2005, at 11:20 AM, mmalcolm crawford wrote:
On Aug 21, 2005, at 11:15 AM, mmalcolm crawford wrote:
There isn't anything in what you've described so far that should
give rise to the error message you report. To check, I created a
framework etc. following your description, and I don't see the
message. What other configuration are you doing?
And specifically, re:
"I don't receive this error unless I've added or removed something
of type "EntityType" after the program has finished launching,"
At what point do you add or remove the instances?
mmalc
At launch I do something like this in the document:
- (id)initWithType:(NSString *)typeName error:(NSError **)outError
{
self = [super initWithType:(NSString *)typeName error:(NSError
**)outError];
[[[self managedObjectContext] undoManager]
disableUndoRegistration];
[[self managedObjectContext] addVariation:@"Untitled Variation"];
[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] enableUndoRegistration];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doUndoCleanup:)
name:NSUndoManagerDidUndoChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doUndoCleanup:)
name:NSUndoManagerDidRedoChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doUndoCleanup:)
name:NSManagedObjectContextObjectsDidChangeNotification object:nil];
return self;
}
The add variation call is the one of note. It calls (within the
managed object context subclass):
-(void)addVariation:(NSString *)theName{
[(TKVariation *)[NSEntityDescription
insertNewObjectForEntityForName:@"TKVariation"
inManagedObjectContext:self] setName:theName];
}
I then have a table view display method, which does:
- (int)outlineView:(NSOutlineView *)outlineView
numberOfChildrenOfItem:(id)item
{
...
if(item==nil)
{
return [[[self managedObjectContext] theVariations] count];
}
return 0;
}
Here is theVariations method:
-(NSArray *)theVariations{
NSFetchRequest *request = [[[NSFetchRequest alloc] init]
autorelease];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"TKVariation" inManagedObjectContext:self];
id modal = [entity managedObjectModel];
[request setEntity:entity];
NSArray *variationsFound = [self executeFetchRequest:request
error:nil];
return variationsFound;
}
This all works great. However, where it breaks is when the user then
decides to hit a toolbar item that calls addVariation again (this
method is in the document):
-(void)addVariation
{
[[self managedObjectContext] addVariation:@"New Variation"];
[variationOutlineView reloadData];
}
As soon as this method is called (i.e. As soon as the user adds
another variation after the document init) the error starts to be
reported whenever I run the query within theVariations. In addition,
the following removeVariation code will also do it:
-(void)removeVariation
{
NSBeginCriticalAlertSheet(@"Remove Variation?", @"Remove",
@"Cancel", nil, [self window], self, @selector
(reallyRemoveVariation:returnCode:contextInfo:), nil, nil, @"Are you
sure you really wish to remove the variation?");
}
-(void)reallyRemoveVariation:(NSWindow *)sheet returnCode:(int)
returnCode contextInfo:(void *)contextInfo
{
if (returnCode == NSAlertDefaultReturn)
{
...
[[self managedObjectContext] removeVariation:currentVariation];
[variationOutlineView reloadData];
}
}
(Now the context subclass code):
-(void)removeVariation:(id)theVariation
{
[self deleteObject:theVariation];
}
Let me know if you need any more information.
Edit: I tried commenting out a troublesome line that I've been
working on:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(doUndoCleanup:)
name:NSManagedObjectContextObjectsDidChangeNotification object:nil];
(in the document init)
This seems to have fixed the problem.
Here is the doUndoCleanup method:
- (void)doUndoCleanup:(NSNotification *)notification
{
[resourceTableView tableViewSelectionDidChange:nil];
[resourceTableView display];
}
This opens a whole different can of worms if something in the
resourceTableView (which is separate from the variations outline
view) is interfering. I'll have a look at the datasource methods and
see if anything is up in those. The whole doUndoCleanup method really
needs to be nicified anyway. It's just basically a method to redraw
everything when there is any for free change made to the context and
the UI will need to catch up.
The stack was still saying the problem was within the outline view
reloading. I'll take a look and report back with issues.
_______________________________________________
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