Search code examples
iosnsnotificationcenternsnotifications

ios NSNotification not received in the handler


I'm using this as the observer:

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
      selector:@selector(newConnection:)
       name:NSFileHandleConnectionAcceptedNotification
         object:nil];

This does get called though when it does the app crashes with this message

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CALayerArray newConnection:]: unrecognized selector sent to instance 0x76aed70'

This is the newConnection handler which is currently empty:

- (void)newConnection:(NSNotification*)notification
{
}

It is also declared in the .h file correctly...

this is the code which is calling the notification

 socketPort = [[NSSocketPort alloc] initWithTCPPort:portNumber];
    int fd = [socketPort socket];
    fileHandle = [[NSFileHandle alloc] initWithFileDescriptor:fd
                                    closeOnDealloc:YES];
...
[fileHandle acceptConnectionInBackgroundAndNotify];

edit: the whole thing above is in a class I've made. Everything except the newConnection is inside this function:

- (id)initWithPortNumber:(int)pn delegate:(id)dl
{
if( self = [super init] ) {
...
}

And I called this file in the viewController like this:

    SimpleHTTPServer *server= [[SimpleHTTPServer alloc]initWithPortNumber:80 delegate:self];

Solution:
The problem was: the view was dealloced because of the arc system
the [fileHandle acceptConnectionInBackgroundAndNotify]; Wasn't really a loop that arc detected to it treated the view as idle and automatically dealloced it so I just made a small timer running with a loop every second that leads to an empty method. Which fixed it.


Solution

  • The problem was: the view was dealloced because of the arc system the [fileHandle acceptConnectionInBackgroundAndNotify]; Wasn't really a loop that arc detected to it treated the view as idle and automatically dealloced it so I just made a small timer running with a loop every second that leads to an empty method. Which fixed it.