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

  • Home
  • SEARCH
  • 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 6628503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:08:20+00:00 2026-05-25T22:08:20+00:00

Perfect KVO here includes two parts: add observer correctly and remove observer correctly. The

  • 0

Perfect KVO here includes two parts: add observer correctly and remove observer correctly.

The story:

  1. I use one UITableViewCell(cell) to display one NSManagedObject(object).
  2. Each object has some dynamic properties that need observing by its cell.
  3. Not all objects have the same set of observed properties. I add key path observers selectively like this:

    if (object.thumbnail_pic_url)
    [object addObserver:cell forKeyPath:@”thumbnail_picture” options:0 context:NULL];

  4. Object could be deleted. I must remove observers when object is deleted. The database is very large and complex so I definitely don’t want to register all cells to receive moc notifications like NSManagedObjectContextObjectsDidChangeNotification. But I can accept to add a cell ivar in object if I have to, even though it goes agains good Modle-View-Controller design pattern.

The problem: How can I correctly remove the observer(cell) for all the registered key paths from an object when it is deleted?

In fact, it is a big problem that can be divided into two small problems:

  1. Where is the best place to put the observer removing code?
  2. How do I determine which key paths to unregister? I can’t query its properties after an object is deleted — it will cause unfulfillable faults, so I can’t write code like this:

    if (object.thumbnail_pic_url)
    [object removeObserver:cell forKeyPath:@”thumbnail_picture”];

and I can’t either blindly remove observer for unregistered key path — exceptions(Cannot remove an observer for the key path “thumbnail_picture” from because it is not registered as an observer.) will be thrown up.

  • 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-05-25T22:08:20+00:00Added an answer on May 25, 2026 at 10:08 pm

    an0,

    There is an NSManagedObject method just for doing deletion timed functions: -prepareForDeletion.

    Its documentation claims: “You can implement this method to perform any operations required before the object is deleted, such as custom propagation before relationships are torn down, or reconfiguration of objects using key-value observing.”

    You could also look at using: -willTurnIntoFault and -didTurnIntoFault. But I think you’ll be happier using -prepareForDeletion.

    Andrew

    P.S. This method is documented in the class reference. I respectfully suggest that you save time by reading the documentation.

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

Sidebar

Related Questions

Here's a perfect example of the problem: Classifier gem breaks Rails . ** Original
Design -- in a perfect world I have one abstract base class A with
My page is perfect with a doctype, and breaks horribly when one is applied.
How can I find perfect power of two between two numbers? Sample input: 0
My one layout works perfect on 1.6+ but throws error on 1.5 and results
My layout was perfect until I decided to add a scrollview. <?xml version=1.0 encoding=utf-8?>
Everything looks perfect when I use the ImageView to show a png image like
They say the perfect test just includes the test framework and the class being
The following code works almost perfect, thanks to the help received here : import
I need the perfect algorithm or C# function to calculate the difference (distance) between

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.