Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8872635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:15:59+00:00 2026-06-14T18:15:59+00:00

I want to implement behaviour similar to NSNotificationCenter ‘s -addObserverForName:object:queue:usingBlock: . Using a method

  • 0

I want to implement behaviour similar to NSNotificationCenter‘s -addObserverForName:object:queue:usingBlock:. Using a method called something like

- (void)addRefetchObserver:(id)observer
                   handler:(FJRefetchHandler)handler;

a block should be stored for later invokation (FJRefetchHandler is defined like this: typedef void(^FJRefetchHandler)(void).

Because I want to remove the block later, I also store observer, and declare the following method:

- (void)removeRefetchObserver:(id)observer;

Usage would look like this:

// some place in code
[controller addRefetchObserver:self handler:^{
    // refetch some stuff, i.e.
    self.data = [self updateData];
}];    
// some other place in code:
[controller removeRefetchObserver:self];

My question is: How should I implement -addRefetchObserver:handler: so that I won’t create any retain cycles? How should I store observers and handlers?

Apparently, NSNotificationCenter somehow stores the observer without retaining it – otherwise I would not be able to call [center removeObserver:self] in -dealloc because -dealloc would never get called.

Also, is there way to get around using __unsafe_unretained when referencing self in the block? i.e. like so:

__unsafe_unretained MyObject *blockSelf = self;
[controller addRefetchObserver:self handler:^{
    blockSelf.data = [blockSelf updateData];
}];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T18:16:00+00:00Added an answer on June 14, 2026 at 6:16 pm

    Apparently, NSNotificationCenter somehow stores the observer without
    retaining it – otherwise I would not be able to call [center
    removeObserver:self] in -dealloc because -dealloc would never get
    called.

    Yes, they keep a weak reference to it. You can easily keep weak references to observers in your classes too: if you need to have a collection of weak references, you can make a non-retaining version of NSArray or NSSet or NSDictionary, using the Core Foundation functions to create CFArray / CFSet / CFDictionary (they are toll-free bridged with, i.e. the same as, the NS equivalents), which allow you to explicitly specify the retaining/releasing behavior; so to not have a strong reference, you just have it do nothing on retain and release.

    That you are storing the observer is kind of weird. With NSNotificationCenter, they store the observer because they need both the observer and selector to make the call. With yours, the block is already enough to make the call, and the block encapsulates all the logic of using the observer, so to store separately an “observer” seems strange. It seems like the only reason you have it is to have a way to remove it. For that matter, it could be any object, as long as you pass the same one in to add as you do to remove.

    Whereas NSNotificationCenter has just one reference to the “observer”, your system has two references — one as the “observer” passed in, but you also have a reference to the block, which in all likelihood has a reference to the “observer” also. If you want it to work the same way as NSNotificationCenter, you need to make sure that these are both weak references. I think you’ve figured this out — the direct “observer” reference you keep weak using what I described in the first paragraph; the block’s reference to the “observer” must also be weak.

    Also, is there way to get around using __unsafe_unretained when
    referencing self in the block? i.e. like so:

    What you have is the correct way to weak-reference something from a block. More specifically, you should use __weak if you are using ARC and targeting only iOS 5+. You should use __unsafe_unretained (like you have) if you are using ARC otherwise. You should use __block if you are not using ARC.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to implement file uploading with behaviour which is commonly seen in various
I want to implement a behaviour for my application that at the start checks
i want to implement the following behaviour: if the users browser-language is e.g. EN
I want to implement a behavior similar to Whatsapp, where when the user can
I'd like to implement a set of similar attached behaviors for use in a
There are similar posts here. I just want to clarify something. I implemented this
I want to implement a docking library in wpf with behavior similar to Adobe
I want to implement a very similar functionality to what Apple did in the
I want to implement the same behaviour of the notification view in the official
I miss the HTML-cellspacing in WPF. Im trying to implement something similar in WPF's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.