Professional Web Applications Themes

NSNotificationCenter - multiple notifications - Mac Programming

I am using a custom view which registers for files dropped on it with the following code (in its initWithFrame:) [self registerForDraggedTypes:[NSArray arrayWithObjects:NSFilenamesPboardType, nil]]; I then post a notification to the default notification centre from - performDragOperation:(id <NSDraggingInfo>)sender method as follows - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender { <snip> [[NSNotificationCenter defaultCenter] postNotificationName:"SBFDropped" object:self userInfo:theNotification]; <snip> } I have an observer class which receives the notification and takes the appropriate action. From its awakeFromNib: [[NSNotificationCenter defaultCenter] addObserver:self selector:selector(sbfDropped:) name:"SBFDropped" object:nil]; Now for my problem, if the action taken results in an alert panel being displayed then I get the notification twice. I mean that ...

  1. #1

    Default NSNotificationCenter - multiple notifications


    I am using a custom view which registers for files dropped on it with the
    following code (in its initWithFrame:)

    [self registerForDraggedTypes:[NSArray
    arrayWithObjects:NSFilenamesPboardType, nil]];

    I then post a notification to the default notification centre from -
    performDragOperation:(id <NSDraggingInfo>)sender method as follows

    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    {
    <snip>

    [[NSNotificationCenter defaultCenter]
    postNotificationName:"SBFDropped" object:self userInfo:theNotification];

    <snip>
    }

    I have an observer class which receives the notification and takes the
    appropriate action.

    From its awakeFromNib:
    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:selector(sbfDropped:) name:"SBFDropped" object:nil];


    Now for my problem, if the action taken results in an alert panel being
    displayed then I get the notification twice. I mean that sbfDropped: is
    called twice for one performDragOperation in the custom view.

    The alert panel is displayed using:
    ..
    ..

    if ( NSRunAlertPanel("File Already Exists", [NSString
    stringWithFormat:"The file % already exists! Do you wish to overwrite?",
    targetFile], "Yes", "No", nil) == NSAlertDefaultReturn)
    [[NSFileManager defaultManager] removeFileAtPath:targetFile
    handler:nil];
    ..
    ..


    As a novice Cocoa developer I would appreciate any advice on what may be
    going on here.


    Jerome Guest

  2. #2

    Default Re: NSNotificationCenter - multiple notifications

    In article <BC064201.8473%isdotpleasedontspamcom>,
    Jerome Reid <isdotpleasedontspamcom> wrote:

    [snip description] 

    I have no idea what's going on, but the best thing to do is to run this
    code in the debugger, put a breakpoint on your sbfDropped: method, and
    find out who's calling it. Once you have that information, things may
    become more clear.
    Michael Guest

  3. #3

    Default Re: NSNotificationCenter - multiple notifications

    Michael Ash <com> wrote:
     
    >
    > I have no idea what's going on, but the best thing to do is to run this
    > code in the debugger, put a breakpoint on your sbfDropped: method, and
    > find out who's calling it. Once you have that information, things may
    > become more clear.[/ref]

    Yes - my suggestion was going to be that maybe performDragOperation is
    being called twice, or maybe there's more than one instance of the
    observer class; anything could be happening, and that's what the
    debugger / logging is for, to find out. m.


    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  4. #4

    Default Re: NSNotificationCenter - multiple notifications

    On 17/12/2003 20:33, in article 1g642t6.1iexqqw1yrp62cN%com,
    "matt neuburg" wrote:
     
    >>
    >> I have no idea what's going on, but the best thing to do is to run this
    >> code in the debugger, put a breakpoint on your sbfDropped: method, and
    >> find out who's calling it. Once you have that information, things may
    >> become more clear.[/ref]
    >
    > Yes - my suggestion was going to be that maybe performDragOperation is
    > being called twice, or maybe there's more than one instance of the
    > observer class; anything could be happening, and that's what the
    > debugger / logging is for, to find out. m.
    >[/ref]


    Thanks Michael and Mat for your quickly supplied suggestions. It is through
    the use of the debugger I ascertained that I was receiving two notification
    for each drop. I decided to to add some NSLog statements and have only
    succeeded in confusing myself further :(.

    To
    - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
    I added the NSLog shown

    [[NSNotificationCenter defaultCenter] postNotificationName:"SBFDropped"
    object:self userInfo:theNotification];

    NSLog("Notification Sent : We have a drop");


    And to sbfDropped I added the NSLog shown:

    NSString *fileName = [[theNotification userInfo] objectForKey:"FileName"];
    NSLog("Notification received - we have a drop");



    The following is part of the console dump

    2003-12-18 00:24:22.410 Sharpbyte Decompressor[1017] Notification received -
    we have a drop
    65535
    55060

    <snip what are these numbers?>

    1065353216
    2003-12-18 00:24:41.766 Sharpbyte Decompressor[1017] Notification received -
    we have a drop
    65535
    <snip>
    1065353216
    2003-12-18 00:24:41.787 Sharpbyte Decompressor[1017] Notification Sent : We
    have a drop
    65535


    Note the notification received twice before the notification sent ?????
    (hence the extra confusion)

    Anyway I think I have to give this some more ysis. I apologise for
    reaching for the quick answer from Usenet. I'm groping a little in the dark
    with this Cocoa stuff and the novice in me hoped it was some silly newbie
    error (which it probably is :) ).

    Thanks again for giving such a quick response to my original post

    Jerome


    PS since it was suggested I examine the call stack I provide it here :

    #0 0x00003fe8 in -[SBDecompressorController sbfDropped:] at
    SBDecompressorController.m:389
    #1 0x909f77d4 in _nsnote_callback
    #2 0x901aa7f0 in __CFXNotificationPost
    #3 0x901af200 in _CFXNotificationPostNotification
    #4 0x909f5620 in -[NSNotificationCenter
    postNotificationName:object:userInfo:]
    #5 0x00004cfc in -[SBDragDropSBFView performDragOperation:] at
    SBDragDropSBFView.m:90
    #6 0x93058ac8 in -[_NSDragManager unregisterDragTypesForWindow:]
    #7 0x92d4c274 in DoDropMessage


    And the second time sbfDropped is called it looks like this

    #0 0x00003fe8 in -[SBDecompressorController sbfDropped:] at
    SBDecompressorController.m:389
    #1 0x909f77d4 in _nsnote_callback
    #2 0x901aa7f0 in __CFXNotificationPost
    #3 0x901af200 in _CFXNotificationPostNotification
    #4 0x909f5620 in -[NSNotificationCenter
    postNotificationName:object:userInfo:]
    #5 0x00004cfc in -[SBDragDropSBFView performDragOperation:] at
    SBDragDropSBFView.m:90
    #6 0x93058ac8 in -[_NSDragManager unregisterDragTypesForWindow:]
    #7 0x92d4c274 in DoDropMessage

    PPS I am on OS 10.3.1 using Xcode 1.0.1 with Cross-develop using target SDK
    MacOSX10.2.7 set.

    Jerome Guest

  5. #5

    Default Re: NSNotificationCenter - multiple notifications

    Jerome Reid <isdotpleasedontspamcom> wrote:
     

    The notification is not being received before it is sent. You are
    sending (posting) the notification before logging that you sent it. So
    naturally the logging for the notification being received happens first.
    You should log *before* posting the notification so that things come out
    in the right order.

    Why is there a 20 second delay between the two "notification received"
    messages?

    What is this thing "theNotification" that you're putting in the
    userInfo? Is there some *other* notification going on here you're not
    telling us about? Why not actually show your code? I have a feeling
    you're not telling the whole story. Maybe that *other* notification is
    the one triggering this first receipt.

    m.

    --
    matt neuburg, phd = com, http://www.tidbits.com/matt/
    AppleScript: The Definitive Guide
    http://www.amazon.com/exec/obidos/ASIN/0596005571/somethingsbymatt
    Read TidBITS! It's free and smart. http://www.tidbits.com
    matt Guest

  6. #6

    Default Re: NSNotificationCenter - multiple notifications

    On 18/12/2003 05:36, in article 1g64re3.1hw7cxjg27moeN%com,
    "matt neuburg" wrote:

     
    >
    > The notification is not being received before it is sent. You are
    > sending (posting) the notification before logging that you sent it. So
    > naturally the logging for the notification being received happens first.
    > You should log *before* posting the notification so that things come out
    > in the right order.
    >[/ref]
    Oops, slightly red face, of course that was the problem there.

    Anyway I found what my problem was so I will describe it here, I hate it
    when I find a description of what I am fighting against in the news archives
    and no solution.

    I was registering for the notification in my observer class awakeFromNib as
    follows:

    From its awakeFromNib:

    [[NSNotificationCenter defaultCenter] addObserver:self
    selector:selector(sbfDropped:) name:"SBFDropped" object:nil];

    But this observer is also a 'File's Owner' for a Nib which controls my
    preference panel (implemented as the "Managing Multiple Nibs" section in the
    O'Reilly book "Cocoa Applications A Step-by-Step Guide" by Garfinkel and
    Mahoney) so when that Nib is loaded it seems the awakeFromNib is called a
    second time, I assumed it would only ever be called once. The
    addObserver:selector:name:object message is therefore sent a second time and
    the defaultCenter now has two entries for my observer.

    Hope that makes sense because I moved the above call to the class init and
    things work as I expect now.

    Jerome

    Jerome Guest

  7. #7

    Default Re: NSNotificationCenter - multiple notifications

    In article <BC07C298.85B4%isdotpleasedontspamcom>,
    Jerome Reid <isdotpleasedontspamcom> wrote:
     

    The cirstances in which awakeFromNib is called are well-doented.
    Basically, it's called when an object is instantiated from a nib, and
    it's called on a nib's owner when a nib is instantiated. There's nothing
    magical going on behind the scenes to make sure that it only gets called
    once. It's very atypical to have an object both be in a nib and be owner
    of another nib.
    Michael Guest

Similar Threads

  1. Email Notifications
    By Brian_WBoE in forum Macromedia Contribute General Discussion
    Replies: 0
    Last Post: October 26th, 06:47 PM
  2. Email Notifications of Events NOT Working...
    By Hari in forum Oracle Server
    Replies: 2
    Last Post: October 28th, 10:41 PM
  3. ASP.NET server notifications
    By sundeep in forum ASP.NET General
    Replies: 1
    Last Post: August 8th, 06:53 AM
  4. Dynamic Controls & Event Notifications
    By Null in forum ASP.NET General
    Replies: 0
    Last Post: July 16th, 10:39 PM
  5. Long Operations User Notifications
    By szabelin in forum ASP.NET General
    Replies: 1
    Last Post: June 28th, 09:17 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139