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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:18:30+00:00 2026-06-11T17:18:30+00:00

FACTS: I have a method executing on a background thread : [currentGame performSelectorInBackground:@selector(playOn:) withObject:self];

  • 0

FACTS:

I have a method executing on a background thread :

[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];

This method basically contains a while loop that keeps on executing until the user clicks on a Quit button:

-(void) playOn: (UIViewController*) thisViewController
{
    while(!quitButtonPressed)
    {
       // this is my method's main loop
    }
}

PROBLEM:

If the user clicks on Quit somewhere in the middle of the above loop
the rest of the loop would have to execute before it checks the BOOL once again and eventually stops. In order to prevent that from
happening and have the while-loop stop as soon as the user clicks on
Quit, I guess I could also add many if(quitButtonPressed) break;
here and there in my while loop in order to semi-constantly check and “immediately” break away if needed. However, this doesn’t seem
very clever or practical from a design perspective given the size of
the above main while-loop and the fact that it contains many smaller
while-loops
inside of it (the number of if.. break; I would have to
add would be quite big and could make things quite complicated to
figure out..)

POSSIBLE SOLUTION (but is it the right one?) :

So I was thinking that the best way would be to stop/cancel the
background thread on which the above method’s while loop is executing,
instead of the while-loop itself, inside the method, the moment the
user clicks on Quit

Is this, or something similar (i.e. a better suggestion), possible and
how exactly could I do this?

POSSIBLE IMPLEMENTATION OF ABOVE SOLUTION:

I could create this new method:

-(void)checkQuitButton
{
   while(!quitButtonPressed)
   {
      //wait
   }
   if(quitButtonPressed)
   {
     // stop this thread-->[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
     // this is the method I'm looking for
   }
}

And then I could start executing the above and my previous main method concurrently on two separate background threads as follows:

[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];
[currentGame performSelectorInBackground:@selector(checkQuitButton) withObject:nil];

While the game while-loop is being executed another while-loop is checking the QuitButton at the same time. But is there a method that I can actually call in order to cancel what was started here:

[currentGame performSelectorInBackground:@selector(playOn:) withObject:self];

?

  • 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-11T17:18:31+00:00Added an answer on June 11, 2026 at 5:18 pm

    The correct solution is to periodically check for a “stop” flag. Abruptly terminating a thread provides no opportunity to clean up resources. In short, you would leak memory terribly.

    But the deeper issue is that you almost certainly should not have this kind of thread. It strongly suggests an incorrect design. In iOS, most background operations should take the form of focused operations, implemented either with NSOperation or blocks with Grand Central Dispatch. You should very, very seldom need a long lived thread that is performing many different kinds of functions. Within your operation, it should be fairly straightforward where to put the “check for cancel” statements.

    There is also almost no case where you should use performSelectorInBackground:. It is an incredibly dangerous method that gives you very little control. Instead, read the Concurrency Programming Guide for guidance on how to properly implement background operations. Pay special attention to the section “Migrating Away From Threads.”

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

Sidebar

Related Questions

I have a dimension (SiteItem) has two important facts: perUserClicks perBrowserClicks however, within this
Facts I have an mvc project. I am using jquery. The problem $('#ModalData').empty(); is
What I'm trying to do is rather basic, but I might have my facts
I have a few questions that I hope clarity and facts can be fed
I have a need for a counter of type long with the following requirements/facts:
Consider a grocery store scenario (I'm making this up) where you have FACT records
I'll preface this with the fact that I'm new to RoR. I have a
i have this snippet of code that use an iterator on a list for
I have a wicket page , which contains two Spring-managed beans , one is
SharedPreferences have method getAll, but it returns no entries despite the fact some keys

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.