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

Similar Threads
thread Thread Starter Forum Replies Last Post
iPhone: I cannot receive any twitter push notification or update the setting of notification. tkwlee iOS 7 8 Aug 31, 2014 07:16 PM
How to change number of times notification repeats itself tg2063 iPhone Tips, Help and Troubleshooting 5 Nov 18, 2013 06:22 AM
DHCP Lease times (Short vs Long times) LFMNX Mac Peripherals 2 Apr 17, 2013 12:01 AM
Getting notification AND email for Shared Calendar, I only wait notification sflomenb iCloud and Apple Services 0 Nov 27, 2012 10:00 PM
anyone know what is the rss feed notification plugin called? ipos Jailbreaks and iOS Hacks 1 Jun 2, 2012 09:59 AM

Forum Jump

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

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

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