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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:01:05+00:00 2026-05-20T18:01:05+00:00

I have a view which loads data via an NSOperation within an NSOperationQueue. I

  • 0

I have a view which loads data via an NSOperation within an NSOperationQueue. I want to allow users to leave this view before the operation has completed. My problem is that I can’t seem to consistently do this without crashing. Here is my code to start the operation:

NSOperationQueue* tmpQueue = [[NSOperationQueue alloc] init];
self.queue = tmpQueue;
[tmpQueue release]; 
SportsLoadOperation* loadOperation = [[SportsLoadOperation alloc] init];
[loadOperation addObserver:self forKeyPath:@"isFinished" options:0 context:NULL];
[self.queue addOperation:loadOperation];
[loadOperation release];    

If I leave the view while the operation is still executing, I often get this error:

[SportsViewController retain]: message sent to deallocated instance 0x38b5a0

If I try to remove the observers so that this doesn’t occur, like this:

-(void)viewWillDisappear:(BOOL)animated {
    if (self.isLoadingData) {
        for (NSOperation *operation in [self.queue operations]) {
            if([operation isExecuting]) {
                [operation cancel];
                [operation removeObserver:self forKeyPath:@"isFinished"];
            }
        }
    }
    [super viewWillDisappear:animated];
}

Then I sometimes get this error:

Terminating app due to uncaught exception 'NSRangeException', reason:
'Cannot remove an observer <SportsViewController 0x661c730> for the key path "isFinished" from <SportsLoadOperation 0x66201a0> because it is not registered as an observer.'

How can I avoid these problems?

  • 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-20T18:01:05+00:00Added an answer on May 20, 2026 at 6:01 pm

    The 2nd error message says it all.

    Have you tried to not removeObserver after [operation cancel] and see what happens then?

    Have you tried to first removeObserver and only then cancel the operation?

    These might help to narrow down the conditions that trigger the error. Also, you might want to add log output to the code to see when it actually executes.

    And, like freespace’s answer says, adding & removing observers is best done in the construction / destruction methods of the observed instances. This generally yields more stable code.

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

Sidebar

Related Questions

I have a View A, which loads a subview B which loads a subview
I have a view which contains a form, the form posts and the data
I have a UIViewController subclass which is having its view created programmatically within the
I have a listview which loads its data from sqlite database. Each row in
I have Resorts.aspx which loads content from ResortProducts.ascx into a div via Ajax. Resorts.aspx
I have a small APP which allows users to view information on Beers and
I have a UITableView which loads through it's navigationController a new viewcontroller. This code
In my application I have an EntryServlet which loads default data and sets up
I have a view which is composed of top, left and bottom headers and
I have a view which was working fine when I was joining my main

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.