Looked at it and thought, then remembered this from the NSNotificationCenter docs: Note: If the default NSNotificationCenter is the last object in your application with a reference to either an object registered to receive notifications or an object being observed, that object will be garbage collected.

So, since I was just making the observers in the addObserver call, they didn't have a reference anywhere other than by the defaultCenter . . . so it was garbage collected. By simply making a reference in the method, then everything worked appropriately.

Wow, so don't go and make some omniscient observer and just cast it off into space to do its thing without some kind of persistent reference.

On Sep 4, 2009, at 10:13 AM, John Larson wrote:

Hi All,

I am putting some unit tests together and am running upon an oddity that I don't understand (and trying to tell myself that WebObjects isn't being end-of-lifed because everything is "legacy" documentation - I'm freaking out!). Anyways, if I do this:

public class PurchasingTest extends XTest {
        
        public class ECLogger{
                public void logECMsg(NSNotification n){
                        Logger.getLogger(this.getClass()).debug("EC sent " + 
n.name());
                }
        }
        
        @Test
        public void simpleECChangeTest(){
                EOEditingContext ec = ERXEC.newEditingContext();
                ec.lock();
                NSNotificationCenter.defaultCenter().addObserver(new ECLogger(),
                                new NSSelector("logECMsg", new 
Class[]{NSNotification.class}),
                                null,
                                ec);
                User user = Utils.newEntityForClass(ec, User.class);
                ec.saveChanges();
        //      NSNotificationCenter.defaultCenter().addObserver(new ECLogger(),
        //                      new NSSelector("logECMsg", new 
Class[]{NSNotification.class}),
        //                      null,
        //                      ec);
                user.setNickname("friend");
                ec.saveChanges();
                ec.unlock();
        }
}

I get nothing in the log. If I switch the comments to add the observer at the latter spot, then I get this in the console, which looks okay to me:

7170 [main] DEBUG tests.PurchasingTest$ECLogger - EC sent EOEditingContextWillSaveChanges 7171 [main] DEBUG tests.PurchasingTest$ECLogger - EC sent EOObjectsChangedInStoreNotification 7171 [main] DEBUG tests.PurchasingTest$ECLogger - EC sent EOObjectsChangedInEditingContextNotification 7171 [main] DEBUG tests.PurchasingTest$ECLogger - EC sent ObjectsWillChangeInEditingContext 7181 [main] DEBUG tests.PurchasingTest$ECLogger - EC sent EOEditingContextDidSaveChangesNotification 7183 [main] DEBUG tests.PurchasingTest$ECLogger - EC sent EOObjectsChangedInStoreNotification 7183 [main] DEBUG tests.PurchasingTest$ECLogger - EC sent EOObjectsChangedInEditingContextNotification

I don't get it. Any ideas? I thought maybe it had something to do with the save, but the fact is that I am running the exact same routine with the same EO data and everything. The only thing that is different is where I add the observer. I am on 5.3 still and using Wonder for 5.3.

Thanks in advance,
John
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/the_larsons%40mac.com

This email sent to [email protected]

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to