I had a similar one the other week, same idea, notification I wanted to remove 
in its handler. 

I assume that before you added __block you were getting an exception in the 
block handler, EACCESS was mine I think. 

I also changed to a block variable and it started working. My presumption was 
that observer was being captured as nil, or some unassigned value into the 
block as the method was entered and thus was rubbish when the callback came and 
as it was not a block variable the later assignment did not change the value 
captured in the block.  I also assumed that making it a block variable allowed 
the captured observer to be changed to the result of addObserver:.. at the end 
of the method call and be available to the block. 

And yes I now love blocks too, especially with all the asynchronous methods 
icloud uses. It's great in an open callback for a document to throw a block on 
the main queue to close it again on the next cycle, and do something else when 
the close callback comes. 

On 16 Feb, 2012, at 4:58, Matt Neuburg <m...@tidbits.com> wrote:

> This appears to be working from all points of view (thank you, Instruments!):
> 
>    MyMandelbrotOperation* op = 
>        [[MyMandelbrotOperation alloc] initWithSize:self.bounds.size 
>                                             center:center zoom:1];
>    __block __weak id observer = [[NSNotificationCenter defaultCenter] 
>      addObserverForName:@"MyMandelbrotOperationFinished" 
>      object:op queue:[NSOperationQueue mainQueue] 
>      usingBlock:^(NSNotification *note) {
>        MyMandelbrotOperation* op2 = note.object;
>        CGContextRef context = [op2 bitmapContext];
>        if (self->bitmapContext)
>            CGContextRelease(self->bitmapContext);
>        self->bitmapContext = (CGContextRef) context;
>        CGContextRetain(self->bitmapContext);
>        [self setNeedsDisplay];
>        [[NSNotificationCenter defaultCenter] removeObserver:observer 
>            name:@"MyMandelbrotOperationFinished" object:op2];
>    }];
>    [self.queue addOperation:op];
> 
> This is delightful. I'm not leaking self, my operations are being dealloced 
> in good order, I'm successfully registering and deregistering, I'm 
> trampolining to the main thread without writing a trampoline method, I'm 
> avoiding the nightmare of storing my observers in an instance variable, op 
> and op2 are the same object - everything about it seems to be okay. My 
> questions are:
> 
> * Is this really an okay way to talk?
> 
> * Why was I crashing until I said __block?
> 
> m.
> 
> --
> matt neuburg, phd = m...@tidbits.com, http://www.apeth.net/matt/
> pantes anthropoi tou eidenai oregontai phusei
> Programming iOS 5! http://shop.oreilly.com/product/0636920023562.do
> RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html
> TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
> 
> 
> 
> _______________________________________________
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> Please do not post admin requests or moderator comments to the list.
> Contact the moderators at cocoa-dev-admins(at)lists.apple.com
> 
> Help/Unsubscribe/Update your Subscription:
> https://lists.apple.com/mailman/options/cocoa-dev/rols%40rols.org
> 
> This email sent to r...@rols.org

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to