Frage

Two objects were working just fine receiving NSNotifications before with XCode 5.1 (and CorePlot 1.4). However, with XCode 5.1.1 (Coreplot 1.5) I found that if the addObserver call is inside the init method, it won't actually be registered (just for those two classes, it works in the parent object of a different class). I even put a NSLog before and after the call to make sure the code was still working. The objects are both strongly referenced by their shared parent, and their parent receives the necessary notifications without issue. And, I have a NSLog in the dealloc where the removeObserver is -it is not called early, as the objects are retained properly.

//The object is a CorePlot CPTGraphHostingView
- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        //other code setting up a few private ivar primitive arrays and values

        //this is where the call was (there are actually 3 Observer calls made)
        [[NSNotificationCenter defaultCenter]
        addObserver: self
        selector: @selector(recordUpdated)
        name: @"recordUpdated"
        object: nil];

        //then this is called before the end
        [self prepareGraph];

        //this is where I moved it
        [self startListening];
    }
    return self;
}

Again, nothing has changed about the code for the object sending the notification or those two classes since I last compiled it with XCode 5.1 (and CorePlot 1.4). And this same call is used in the init of the parent, and works perfectly. My only solution last night was to refactor out the addObserver calls into a new method, and call that method at the end of init.

However, I don't understand why this was necessary. Can anyone think of a reason that addObserver placed in the middle of init would be "ignored", but when inside another method called by init, it works?

Edit, and notes:

I added more code to show the init. I also added notes that it is a graph view for CorePlot, and the CorePlot was also recently updated to 1.5 (I forgot about that) -which could be the source of the problem.

lead_the_zeppelin's suggestion that it is being reassigned, seems possible. But wouldn't the call from NSNotificationCenter to a -now- dealloc'd object crash the program (note, dealloc is NOT called - I have a NSLog there)? I could easily test this by printing the object instance string inside the init, and afterwards. I did test this last night, asking for the instance, but I don't think I copied that bit into the init.

War es hilfreich?

Lösung

The Core Plot hosting view removes itself as an observer of all notifications whenever you set the hosted graph or the collapsesLayers property. Please post a bug report on the issue tracker if you'd like us to fix it.

In the meantime, add your observers after you set up the graph.

Andere Tipps

self is probably being reassigned after you subscribe to the notification center. Can you move the code to

-(void)awakeFromNib
{
    [[NSNotificationCenter defaultCenter]addObserver: self  selector:@selector(recordUpdated) name:@"recordUpdated" object:nil];
}

or maybe to -viewDidLoad ?

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top