Re: bug in NSPersistentDocument tutorial?
Re: bug in NSPersistentDocument tutorial?
- Subject: Re: bug in NSPersistentDocument tutorial?
- From: mmalcolm crawford <email@hidden>
- Date: Sun, 30 Apr 2006 10:28:33 -0700
On Apr 20, 2006, at 9:31 AM, Jim Balhoff wrote:
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.
Thanks for catching this. There are actually two issues here.
Since the document retains the department object, you must make sure
that it is properly released in the case of the document being
reverted. You need to implement a revertToContentsOfURL:ofType:error:
method, as follows:
- (BOOL)revertToContentsOfURL:(NSURL *)inAbsoluteURL ofType:(NSString
*)inTypeName error:(NSError **)outError
{
[self setDepartment: nil];
return [super revertToContentsOfURL:inAbsoluteURL
ofType:inTypeName error:outError];
}
Note that although technically correct, due to a bug in
NSObjectController the above is still not sufficient. The object
controller maintains a handle to the department object even after a
revert, so you must also set its content to nil. To work around this,
you need to add an outlet to the controller and then connect it in
Interface Builder.
// add outlet as an instance variable in MyDocument.h
IBOutlet NSObjectController *departmentController;
- (BOOL)revertToContentsOfURL:(NSURL *)inAbsoluteURL ofType:(NSString
*)inTypeName error:(NSError **)outError
{
[departmentController setContent:nil];
[self setDepartment:nil];
return [super revertToContentsOfURL:inAbsoluteURL
ofType:inTypeName error:outError];
}
mmalc
_______________________________________________
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