Attaching and Detaching Observers to a document, what is the correct way?
Hi,
I came across below code snippet in many of the sample PLugins where Observer is being:
- Attached on the kAfterOpenDocSignalResponderService
- Detached on the on kBeforeCloseDocSignalResponderService
Same DocRef is being used in both the cases but the Observer being referenced is new each time, so is it a correct way to do it? and Will it work?
GTTxtEdtResponder::attachDetachStoryObserver(UIDRef storyUIDRef, bool16 bAttach)
{
ErrorCode status = kFailure;
do{
// while we observe all stories (whether they are accessible or not), we only let accessible
// stories affect the cache. See the observer implementation for this logic.
InterfacePtr<IObserver> iGTTxtEdtObserver(storyUIDRef,IID_IGTTXTEDTSTORYOBSERVER); //<--new reference created
if (iGTTxtEdtObserver == nil){
ASSERT_FAIL("GTTxtEdtResponder::attachDetachStoryObserver - no observer on story?");
break;
}
bAttach ? iGTTxtEdtObserver->AutoAttach() :iGTTxtEdtObserver->AutoDetach();
status = kSuccess;
}while (kFalse);
return status;
}
If you look closely, you can see that every time a detach is being called after attach, a new Observer reference is created.
Is correct way to do it?