GithubHelp home page GithubHelp logo

Comments (1)

chaitanyagupta avatar chaitanyagupta commented on August 25, 2024

No longer needed. Use FXNotifications instead.

Quoting myself from earlier:

This solves two main problems with block based observers: 1) Inadvertently retaining self, and 2) Forgetting to remove the block based observation (which requires one to retain the returned observer object). The combination of 1 and 2 can lead to an even bigger memory leak where self is retained for the lifetime of the application. See [1] for more details.

So, for example, you could simply write this in init (if that’s where you want to observe the notification):

[[NSNotificationCenter defaultCenter] addObserver:self
                                         forName:NSSomeNotificationName
                                          object:nil
                                           queue:[NSOperationQueue mainQueue]
                                      usingBlock:^(NSNotification *note, id self) {
                                                     NSLog(@"self: %@", self); // look, no leaks!
                                                 }];

And be rest assured that the observation will go away when the “observer” is deallocated. It also returns an observation token just like the original API which can be used to manually remove the observation (e.g. when you need to observe/unobserve notifications in -viewWillAppear: and -viewDidAppear: respectively).

Also, because we shadow self in the observation block, there’s no danger of a leak (unless you reference an ivar directly!)

  1. http://sealedabstract.com/code/nsnotificationcenter-with-blocks-considered-harmful/

from chocolate.

Related Issues (9)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.