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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:15:31+00:00 2026-05-28T08:15:31+00:00

When I cancel an NSOperation (when user presses a button) cancel method is called

  • 0

When I cancel an NSOperation (when user presses a button) cancel method is called from the main thread, but evidently the operation is running in another thread.
So, to avoid race conditions when I change _isExecuting and _isFinished, I think cancel (or at least its logic) should be called from the same thread that the NSOperation. Apart from that, when user cancels it, several files are deleted and it takes time. Because cancel is called from main thread, all the app becomes unresponsive for a while, which is ugly.

How can I execute cancel code in the same thread that the current NSOperation?

I tried this in cancel (similar to what I saw in ASIHTTPRequest):

if (_operationThread) {
    [self performSelector:@selector(cancelOnRequestThread) onThread:_operationThread withObject:nil waitUntilDone:NO];
} else {
    [self cancelOnRequestThread];
}

And _operationThread is setted in start method using:
_operationThread=[NSThread currentThread];

But it doesn’t work.

Any idea or suggestion?

Note: I use concurrent operations, so I use start instead of main.

Thanks a lot for help.
Ricardo.

  • 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-28T08:15:32+00:00Added an answer on May 28, 2026 at 8:15 am

    It’s fine to call cancel on an NSOperation from the main thread. The cancel method is thread-safe.

    That shouldn’t cause any blocking on your main thread because the cancel method itself shouldn’t be doing any work. If you have overridden the cancel method of your operation to delete files, etc then that is the wrong approach. You shouldn’t override the cancel method, instead just check the isCancelled method at regular points within the operation’s main method (e.g. inside any tight loops) and then return from main early if isCancelled returns YES, which will then cancel the operation on the same thread as the rest of the execution.

    If that’s how you’ve implemented it already and you’re still having performance issues, is it possible that your operation is not really running on a background thread at all? For example if you’ve added it to the queue returned by [NSOperationQueue mainQueue] then that’s actually running on the main application thread.

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

Sidebar

Related Questions

Is there an easy way to cancel a curl_easy_perform from another thread?
I want to cancel this operation if the user selects a specific item because
I have 2 buttons Cancel and Next button on the same page but it
Is it possible to cancel an event from within the onKey method. I only
I cancel my operation by calling the CancelAsync() method on the BackgroundWorker, and when
If I need to cancel some operation on a thread, when should I use
How can I cancel a loading operation in WebView started by loadData() method?
I'm using NSOperationQueue , and NSOperation for running some function on background click. But
I have a cancel button which should bring the user back to the start
Is it possible to cancel/abort a running build process in Visual Studio 2008? How?

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.