bug in NSPersistentDocument tutorial?
bug in NSPersistentDocument tutorial?
- Subject: bug in NSPersistentDocument tutorial?
- From: Jim Balhoff <email@hidden>
- Date: Thu, 20 Apr 2006 12:31:00 -0400
Hi,
In the NSPersistentDocument tutorial [1] under "Adding a Department
Object", it is shown how to add a Department as an instance variable
of the document class. I pasted the accessor code from the tutorial
at the end. However, I have found that if you open a saved document,
make some changes, and then choose "Revert" from the "File" menu, the
document will not reopen and the following appears in the run log:
2006-04-20 12:05:15.165 DepartmentAndEmployees[27425] The
NSManagedObject with ID:0x3fb2e0 <x-coredata://D96D3A5A-0375-4FB4-
ADA2-62C148EA7AB8/Employee/p5> has been invalidated.
If, instead of keeping the department as an instance variable, you
"adopt the mediator pattern" as described in that tutorial, Revert
works fine.
I took this approach in my own application and now Revert works
properly. But, for my own education, what would be the proper method
if one did want to add an NSManagedObject as an instance variable in
the document class and not have an invalid reference after Revert?
Thanks,
Jim
[1] <http://developer.apple.com/documentation/Cocoa/Conceptual/
NSPersistentDocumentTutorial/index.html>
- (NSManagedObject *)department
{
if (department != nil) {
return department;
}
NSManagedObjectContext *moc = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSError *fetchError = nil;
NSArray *fetchResults;
@try {
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Department"
inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
fetchResults = [moc executeFetchRequest:fetchRequest
error:&fetchError];
} @finally {
[fetchRequest release];
}
if ((fetchResults != nil) && ([fetchResults count] == 1) &&
(fetchError == nil)) {
[self setDepartment:[fetchResults objectAtIndex:0]];
return department;
}
if (fetchError != nil) {
[self presentError:fetchError];
}
else {
// should present custom error message...
}
return nil;
}
- (void)setDepartment:(NSManagedObject *)aDepartment
{
if (department != aDepartment) {
[department release];
department = [aDepartment retain];
}
}
_______________________________________________
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