Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Jun 25, 2013, 05:02 PM   #1
Val K
macrumors member
 
Join Date: Dec 2012
Location: Italy
saveAction called 2 times with notification

I have a custom class for NSButton called `MyButton` where I post a notification for a quicksave
`MyButton.m`:
Code:
    -(void)mouseDown:(id)sender{
        [super mouseDown:sender];
        [super mouseUp:sender];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"quickSave" object:nil userInfo:nil];
    }
in `AppDelegate` I get the notification for the quick save:
`AppDelegate.m`:
Code:
    - (IBAction)saveAction:(id)sender{
        NSLog(@"Saving...");
        NSError *error = nil;
        if (![[self managedObjectContext] commitEditing]) {
            NSLog(@"%@:%@ unable to commit editing before saving", [self class], NSStringFromSelector(_cmd));
        }
        if (![[self managedObjectContext] save:&error]) {
            [[NSApplication sharedApplication] presentError:error];
        }
    }
    -(void)awakeFromNib{
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveAction:) name:@"quickSave" object:nil];
    }
Via the `NSLog "Saving..."` I see that the saveAction is called 2 times. Why? Thanks in advance
__________________
This signature states that I bought a lot of stuff that makes Apple shareholders happy.
Val K is offline   0 Reply With Quote
Old Jun 25, 2013, 05:34 PM   #2
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by Val K View Post
Via the `NSLog "Saving..."` I see that the saveAction is called 2 times. Why? Thanks in advance
Did you register more than one object for that notification?

In the NSLog statement, log sender and self as well.
gnasher729 is offline   0 Reply With Quote
Old Jun 25, 2013, 05:54 PM   #3
Val K
Thread Starter
macrumors member
 
Join Date: Dec 2012
Location: Italy
no, as you can see there's no specified object, it's "nil".
this is the log:
Code:
Saving...
sender: NSConcreteNotification 0x101915b70 {name = quickSave}
self: <AppDelegate: 0x101927730>
Saving...
sender: NSConcreteNotification 0x101915b70 {name = quickSave}
self: <AppDelegate: 0x1005374c0>
there are two different self. what does it mean?

PS: i've tried this solution but it doesn't work
Code:
-(void)awakeFromNib{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"quickSave" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(saveAction:) name:@"quickSave" object:nil];
}
__________________
This signature states that I bought a lot of stuff that makes Apple shareholders happy.

Last edited by Val K; Jun 25, 2013 at 06:00 PM.
Val K is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Tags
cocoa, nsnotificationcenter, objective-c

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 07:44 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC