NSNotifications

  • Im trying to figure out NSNotifications.

    My Application is a 3D modelling application. i have 4 views of the
    same 3D Scene. When the scene is manipulated, i want the 4 views to
    update their display (in real time, as the mouse is dragged across
    the screen).

    Ive got NSNotification working (i think), but the views only get
    updated when when i let go of the mouse button.

    Ive traced the program, to the point where i know the notification is
    being posted just fine. but is only "broadcast/received" by the
    observers after i let go of the mouse. Is this normal ? is there
    anywhere around this ? cause i do need real-time updates of all views.

    Thanks for your help guys.

    -Aaron
  • What it sounds like is happening is that the notification is being
    sent, but due to the way your application's run loop operates, it's
    not actually being received until the mouse is released.

    In addition, for something you want to update in real time,
    NSNotifications probably aren't the greatest idea.  You are probably
    better off using Key Value Observing, or even getting your model to
    directly tell the views that a change has occurred.

    Mike.

    On 25 Oct 2006, at 15:16, Aaron Boothello wrote:

    > Im trying to figure out NSNotifications.
    >
    > My Application is a 3D modelling application. i have 4 views of the
    > same 3D Scene. When the scene is manipulated, i want the 4 views to
    > update their display (in real time, as the mouse is dragged across
    > the screen).
    >
    > Ive got NSNotification working (i think), but the views only get
    > updated when when i let go of the mouse button.
    >
    > Ive traced the program, to the point where i know the notification
    > is being posted just fine. but is only "broadcast/received" by the
    > observers after i let go of the mouse. Is this normal ? is there
    > anywhere around this ? cause i do need real-time updates of all views.
    >
    > Thanks for your help guys.
    >
    > -Aaron
    > _______________________________________________
    > Do not post admin requests to the list. They will be ignored.
    > Cocoa-dev mailing list      (<Cocoa-dev...>)
    > Help/Unsubscribe/Update your Subscription:
    > http://lists.apple.com/mailman/options/cocoa-dev/mike.abdullah%
    > 40gmail.com
    >
    > This email sent to <mike.abdullah...>
  • Hey Clint,
    here's some pseudo code. and im currently only posting a notification
    when i drag a mouse.....just testing to see if tis would actually
    work the way i want it to.

    my3DObject class: (poster of notification)

    static  NSString *NSObjectDidChange;

    //called when object is changed to post a notification
    -(void)objectDidChange
    {
    [[NSNotificationCenter defaultCenter]
    postNotificationName:NSObjectDidChange object:self];
    }

    myView classes (each view is observer classes):

    - (id)initWithFrame:(NSRect)frameRect
    {
    .
    .
    [[NSNotificationCenter defaultCenter] addObserver:self
                          selector:@selector(viewNeedsUpdate:)
                          name:NSObjectDidChange
                          object:mySuperModel];
    .
    .
    }

    - (void)mouseDragged:(NSEvent *)theEvent
    {
    // change object accoridingly
    .
    .
    [myObject objectDidChange];
    }

    i'm not even posting a notification in the mouseUp method. but it
    only seems to be refreshing the views when i life the mouse button
    (ive commented out all the "[view setNeedsDisplay:YES]" in the code.

    Out of curiosity i put a debugging statement in the
    "objectDidiChange" method...a "printf"...and it just keep coming into
    this method no matter what..weter or not the mouse is pressed or not.

    Thanks.

    On 26/10/2006, at 2:02 AM, Clint Shryock wrote:

    > Aaron-
    >
    > could you be more specific (code snippets) as to where you are
    > sending notifications.
    > it sounds like your posting the notification with mouseUp:(NSEvent
    > *)event
    > instead you would want mouseDragged:(NSEvent *)event
    > without code i can't really tell if my suggestion is relevant or at
    > least not completely stupid, so i could be way off.
    >
    > -Clint
  • couple of questions/thoughts.

    first question:

    static  NSString *NSObjectDidChange;

    does this ever get assigned a value somewhere? if not then you may be
    posting a pointer that points to nothing...
    something like NSObjectDidChange = @"MyObjectChanged"; would be needed.

    even then i don't know where that string is declared so i can only assume
    the receiving object knows what it is. i'll assume you have
    done something similar.

    next your notification seems off.
    you send the notification from my3DObject, notification name
    NSObjectDidChange with object type of my3DObject.
    but in myViewClass you register them to listen for notifications with a name
    of NSObjectDidChange and containing an object of mySuperModel, not
    my3DObject.  so i guess my3dObject is of type mySuperModel? if not this
    could be a problem, or perhaps you just have a typo in your pseudo code.

    if i wanted all views (myViewClass) to listen for a notification from
    my3DObject when the mouse was dragged i would do:

    my3DObject:
    -(void)mouseDragged
    {
    .
    .
    .
    [[NSNotificationCenter defaultCenter]
    postNotificationName:@"CTSObjectDidChange" object: self];
    .
    .
    .
    }

    myViewClass:
    - (id)initWithFrame...
    {
      .
      .
      .
      [[NSNotification defaultCenter] addObserver:self
                                        selector:@selector(viewNeedsUpdate:)
                                        name:@"CTSObjectDidChange"
                                        object:nil];
      .
      .
      .
    }

    doing it this way means my views don't care which object the notification
    came from, just the name "CTSObjectDidChange".
    i used CTS to start because those are my initials, i'm not sure about all
    the rules for naming stuff with NS at the start, there are a few things
    reserved for that.

    the way you are posting below doesn't seem to match up.  this could explain
    why viewNeedsUpdate is not firing when you drag the mouse.
    with out more code i can't really speculate as to why the views magically
    fix them selves on mouse up.

    I'm still learning cocoa so i may be completely wrong, maybe someone else
    can help more.
    anyway i hope i'm not and it helps you somewhere

    -Clint

    On 10/25/06, Aaron Boothello < <hawkeye_a...> wrote:

        Hey Clint,
        here's some pseudo code. and im currently only posting a notification
    when i drag a mouse.....just testing to see if tis would actually work the
    way i want it to.

        my3DObject class: (poster of notification)

        static  NSString *NSObjectDidChange;
        //called when object is changed to post a notification
        -(void)objectDidChange
        {
        [[NSNotificationCenter defaultCenter]
    postNotificationName:NSObjectDidChange object: self];
        }
        myView classes (each view is observer classes):

        - (id)initWithFrame:(NSRect)frameRect
        {
        .
        .
        [[NSNotificationCenter defaultCenter] addObserver: self
                            selector: @selector(viewNeedsUpdate:)
                            name:NSObjectDidChange
                            object:mySuperModel];
        .
        .
        }

        - (void )mouseDragged:(NSEvent *)theEvent
        {
        // change object accoridingly
        .
        .
        [myObject objectDidChange];
        }

        i'm not even posting a notification in the mouseUp method. but it only
    seems to be refreshing the views when i life the mouse button (ive commented
    out all the "[view setNeedsDisplay:YES]" in the code.

        Out of curiosity i put a debugging statement in the "objectDidiChange"
    method...a "printf"...and it just keep coming into this method no matter
    what..weter or not the mouse is pressed or not.

        Thanks.
previous month october 2006 next month
MTWTFSS
            1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31          
Go to today