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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:23:02+00:00 2026-05-26T22:23:02+00:00

I am working on an application where I want to call a method every

  • 0

I am working on an application where I want to call a method every few seconds whilst the user has their finger on a button, and stops on release.

At the moment I am setting off an NSOperation on the Touch Down event, which should then call an NSTimer to fire another NSOperation 2 seconds later.

However only the first “runOperation” is happening; the ones from the timer aren’t.

- (IBAction)buttonPressed:(id)sender
{
    [self doStuff];
}

- (void)runOperation {
    NSInvocationOperation *operation = [[NSInvocationOperation alloc] initWithTarget:self
                                                                            selector:@selector(doStuff)
                                                                          object:nil];

    [queue addOperation:operation];
    [operation release];
}

- (void)doStuff {
    /* stuff goes here */

    [self performSelectorOnMainThread:@selector(setTimer) withObject:nil waitUntilDone:YES];
}

- (void)setTimer
{
    timer = [[NSTimer timerWithTimeInterval:2.f target:self selector:@selector(runOperation) userInfo:nil repeats:NO] retain];
}

- (IBAction)finishTakingPictures:(id)sender {
    [timer invalidate];
    timer = nil;
}
  • 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-26T22:23:03+00:00Added an answer on May 26, 2026 at 10:23 pm

    Man, hope this helps you out, don’t know why I spent so much time on this… 🙂

    If you use NSTimer:timerWithTimeInterval:... you need to add the timer to a run loop using NSRunLoop:addTimer:forMode. That’s why your timer isn’t firing. If you use NSTimer:scheduledTimerWithTimeInerval:... it will be added to the current run loop.

    A few thoughts on your implementation.

    1. You have lots of indirection. You can set it directly in doStuff and get rid of the setTimer method.

    2. There’s no reason to set the timer on the main thread, you can set it on any thread.

    3. You don’t really need a timer at all, I’ll suggest an alternate implementation below.

    ALTERNATE IMPLEMENTATION:

    1. Add a BOOL property e.g. @property (nonatomic) BOOL isFinished;

    2. When the user presses the button, set isFinished = NO; and start your first operation.

    3. When the operation completes, if isFinished == NO start another operation. You can add a delay by using performSelector:afterDelay: or using dispatch_after()

    4. When the user stops pressing the button, set isFinished = YES

    This alternate implementation assumes you want to start the next operation after the first is complete. If you want the operation to happen after a fixed interval independent of whether an operation is already processing change your timer to repeat and when the timer fires check isFinished == YES to invalidate the timer.

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

Sidebar

Related Questions

I am working on a windows application where i want to display orkut user
I have web application where i want to call one method on body onload
i am developing a standalone application using html and want to call method residing
I am working on an MVC2 application and want to set the maxlength attributes
I'm trying to get tags working in my rails application and want to use
I'm working on a conference application where we want the sessions to be first
I am working in an android application and I want to implement a Gallery.
I'm working on building a Silverlight application whereas we want to be able to
I am working on window application using c# . i want to show my
For the application I am working on ,I want my text to be displayed

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.