• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
__block __weak - am I doing this right?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

__block __weak - am I doing this right?


  • Subject: __block __weak - am I doing this right?
  • From: Matt Neuburg <email@hidden>
  • Date: Wed, 15 Feb 2012 12:58:16 -0800

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 = email@hidden, 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 (email@hidden)

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:

This email sent to email@hidden


  • Follow-Ups:
    • Re: __block __weak - am I doing this right?
      • From: Wim Lewis <email@hidden>
    • Re: __block __weak - am I doing this right?
      • From: Roland King <email@hidden>
    • Re: __block __weak - am I doing this right?
      • From: Greg Parker <email@hidden>
  • Prev by Date: Re: receptionist pattern question: NSOperationQueue vs. GCD
  • Next by Date: Re: CGContextSaveGState
  • Previous by thread: Re: Block Behavior
  • Next by thread: Re: __block __weak - am I doing this right?
  • Index(es):
    • Date
    • Thread