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 7409777
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:08:03+00:00 2026-05-29T06:08:03+00:00

I have a ViewController class that has a property that is the model which

  • 0

I have a ViewController class that has a property that is the model which I want to observe as properties on the model change. Within my model object I have a property that is periodically updated in the background of my application. As it updates, I need to execute code within my ViewController.

To do this, I create an observer on my model from within my ViewController viewDidLoad method.

[ModelObject addObserver:self 
              forKeyPath:@"State" 
                 options:NSKeyValueObservingOptionNew 
                 context:nil];

As you can see, this is nothing special and the observation method behaves as it should so long as I leave the view displayed on my screen. If I remove the above view from the parent view, I get an EXC_BAD_ACCESS error message when my ModelObject instance Mode property changes. Specifically, my app crashes at the line that is updating the Mode property and I receive the mostly useless EXC_BAD_ACCESS on the following line of code within the ModelObject instance.

//This is located in a method that periodically toggles the value of "State"
[self setState: 2];

I would assume the solution to this problem is to call [ModelObject removeObserver: self forKeyPath:@"State"] from somewhere within my ViewController when it is removed from it’s parent subview array. However, I have added that line of code within my viewDidUnload method but I’ve found the the viewDidUnload method isn’t being called. I’m not sure if that is the right place, but it needs to go some where.

What might I be doing incorrectly? I know the problem is KVO related because if I remove the observation, the application works without any problem. My model instance can toggle this value as much as it wants and my application never crashes. What should I do to be sure that my observer is removed correctly when the observing view is being removed from it’s parent’s subview array?

  • 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-29T06:08:03+00:00Added an answer on May 29, 2026 at 6:08 am

    I generally like to put the addObserver: and removeObserver: in viewWillAppear: and viewWillDisappear:. I find these are more reliable bookends than viewDidLoad and viewDidUnload.

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

Sidebar

Related Questions

I have a view controller class that has to implement several protocols. Too keep
I have the problem that my view controller class has too many delegates and
I have a ViewController that has its own NIB. I would like to embed
For instance, I have a model class which handles receiving bluetooth messages from other
If I have a custom view controller class that I want to reuse but
In my Brain model I have an NSMutableArray property called stockCars which holds 8
I have a ViewController which has an embedded UIImageView . Another ViewController has a
I have created a certain class to represent an object. My program has several
I have a ViewController class called GamePlay. In GamePlay there is a nested class
I have a viewController (Planner) that loads two view controllers (InfoEditor and MonthlyPlan) when

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.