Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct management of addObserverForName:object:queue:usingBlock:

I'm still new to blocks in objective-c and wondering if I have this psuedo code correct. I'm not sure if it's enough to just remove the observer or if i have to call removeObserver:name:object:

-(void) scan {     Scanner *scanner = [[Scanner alloc] init];     id scanComplete = [[NSNotificationCenter defaultCenter] addObserverForName:@"ScanComplete"                          object:scanner                          queue:nil                          usingBlock:^(NSNotification *notification){                             /*                              do something                              */                             [[NSNotificationCenter defaultCenter] removeObserver:scanComplete];                             [scanner release];                         }];     [scanner startScan]; } 

Update: I'm receiving intermittent EXC_BAD_ACCESS from this block, so this can't be right.

like image 272
seanalltogether Avatar asked Dec 29 '10 01:12

seanalltogether


People also ask

What is Notificationcenter default addObserver?

default — This is the notification variable you can create it globally inside your class if you having more notifications. addObserver(self, — This is for the class where we are going to observer notification.

How do I use notification observer in Swift?

First, register an observer for a notification with: addObserver(_:selector:name:object:) Then, post a notification with post(name:object:userInfo:) … … after which your selector is called. And don't forget to remove the observer with removeObserver()

What is Nsnotification in Swift?

A notification dispatch mechanism that enables the broadcast of information to registered observers. iOS 2.0+ iPadOS 2.0+ macOS 10.0+ Mac Catalyst 13.0+ tvOS 9.0+ watchOS 2.0+


1 Answers

Declare the scanComplete variable before defining the block itself.

The reason why you need to do this is because you're trying to access a variable that doesn't exist within the block at the time of definition since the variable itself has not been assigned yet.

What is EXC_BAD_ACCESS? Well, it's an exception that is thrown when you try to access a reference that doesn't exist. So that is exactly the case in your example.

So if you declare the variable before the block itself, then it should work:

-(void) scan {     Scanner *scanner = [[Scanner alloc] init];     __block id scanComplete;     scanComplete = [[NSNotificationCenter defaultCenter] addObserverForName:@"ScanComplete"                          object:scanner                          queue:nil                          usingBlock:^(NSNotification *notification){                            /*                            do something                            */                            [[NSNotificationCenter defaultCenter] removeObserver:scanComplete];                            [scanner release];                     }];     [scanner startScan]; } 
like image 180
Jacob Relkin Avatar answered Oct 02 '22 12:10

Jacob Relkin