Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeObserver not working

I have next code:

@implementation SplashViewVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.splashView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Default.png"]];
    self.activityIndicator.originY = 355.f;
    [[NSNotificationCenter defaultCenter] addObserverForName:NCDownloadComplete object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *n){
        NSInteger errorCode = [n.userInfo[@"errorCode"] integerValue];        
        [self.activityIndicator stopAnimating];
        if (errorCode == ERROR_CODE_NO_CONNECTION) {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Some problem with server" delegate:self cancelButtonTitle:@"try again" otherButtonTitles:nil];
            [alertView show];
        } else if (errorCode == 0) {
            [self dismissViewControllerAnimated:YES completion:nil];
        }
    }];
    [self downloadData];
}

- (void)downloadData
{
    [self.activityIndicator startAnimating];
    [[Server sharedServer] getMovieData];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self downloadData];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewDidDisappear:animated];
}

@end

So I put breakpoints in begin of viewDidLoad method, in viewDidDisappear. When I launch app that first go to viewDidload, after downloading it is go to viewDidDisappear.

But during my app I again download data and post notification: NSDownloadComplete. And in this VC it is work, but I removed later using:

[[NSNotificationCenter defaultCenter] removeObserver:self]

This VC use viewDidLoad once in the beginning & can not again addObserver.

What is wrong?

EDIT I try put addObserver method to viewWillAppear or viewWillDisappear - no results. I add NSLog(@"addObserver"); before

 [[NSNotificationCenter defaultCenter] addObserverForName...

in viewDidLoad

and write

- (void)viewDidDisappear:(BOOL)animated
{
    NSLog(@"removeObserver");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super viewDidDisappear:animated];
}

In log I see:

2013-06-10 14:32:05.646 myApp[9390:c07] addObserver
2013-06-10 14:32:06.780 myApp[9390:c07] removeObserver

What wrong?

EDIT 2 you can see that observer must be removed but it again run block in addObserver method

enter image description here

like image 358
user2213271 Avatar asked Jun 10 '13 11:06

user2213271


1 Answers

Apart from add/remove observer calls not properly being balanced, at noted in the other answers, there is another problem.

Your code to remove the observer is wrong. For a block-based observer, the return value of addObserver must be given as argument to removeObserver. So you should add a property

@property(nonatomic, strong) id observer;

to the class. Then you add the observer with

self.observer = [[NSNotificationCenter defaultCenter] addObserverForName:NCDownloadComplete object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *n){
    // ...
}];

and remove it with

[[NSNotificationCenter defaultCenter] removeObserver:self.observer];
like image 196
Martin R Avatar answered Oct 14 '22 09:10

Martin R