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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:03:55+00:00 2026-05-28T07:03:55+00:00

I have an NSTimer object that I need to repeatedly execute. The code that

  • 0

I have an NSTimer object that I need to repeatedly execute. The code that it calls is ultimately toggles a setting in my application through 4 various states. Each time the state changes, I want my interval to change on my timer. This loop should continue throughout the lifetime of the application.

As far as I can tell, you can not update the interval property on an NSTimer. I believe this means that I need to add code to create a new NSTimer object each time I toggle my state within my timer code. The following implementation is what I am thinking:

//Consider that in my containing class, there is an NSTimer property, named myTimer.

//My selector method, used by myTimer
-(void) updateState:(NSTimer *) timer
{
    switch (AppState)
    {
    case state_1:
        //Update current state to state_2
        [self setMyTimer:[NSTimer scheduledTimerWithTimeInterval:30.0 
                                                          target:self 
                                                        selector:@selector(updateState:) 
                                            userInfo:nil repeats:NO];
        break;
    case state_2:
        //Update current state to state_3
        [self setMyTimer:[NSTimer scheduledTimerWithTimeInterval:5.0 
                                                          target:self 
                                                        selector:@selector(updateState:) 
                                            userInfo:nil repeats:NO];
        break;
    case state_3:
        //Update current state to state_4
        [self setMyTimer:[NSTimer scheduledTimerWithTimeInterval:30.0 
                                                          target:self 
                                                        selector:@selector(updateState:) 
                                            userInfo:nil repeats:NO];
        break;
    case state_4:
        //Update current state back to state_1
        [self setMyTimer:[NSTimer scheduledTimerWithTimeInterval:5.0 
                                                          target:self 
                                                        selector:@selector(updateState:) 
                                            userInfo:nil repeats:NO];
        break;
    }
}

Is this the most optimal solution or can the creation of all these timers cause any form trouble for my application as it continuously executes?

Also, I have ARC cut on within my project. In this case, will I still need to destroy the previous NSTimer objects?

  • 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-28T07:03:55+00:00Added an answer on May 28, 2026 at 7:03 am

    can the creation of all these timers cause any form trouble for my application as it continuously executes?

    Not as long as you’re managing the memory correctly. Using a synthesized setter will take care of this for you.

    In this case, will I still need to destroy the previous NSTimer objects?

    Not explicitly. When a non-repeating timer fires, it invalidates itself, and the run loop sends release (because it had retained it). If you’ve got a claim on it, you’ll need to release it too, which you do using the setter.

    Since none of these timers are repeating, you could drop the ivar and instead use performSelector:withObject:afterDelay:

    - (void)updateState {
    
        NSTimeInterval delay = 30;
        switch(AppState)
        {
            case 1: { break; }
            case 2: { delay = 5.0;
                      break; }
            case 3: { break; }
            case 4: { delay = 5.0;
                      break; }
        }
    
        [self performSelector:@selector(updateState) 
                   withObject:nil 
                   afterDelay:delay];
    
    }
    

    (By the way, I assume you’ve got breaks in your real code. You definitely should if you don’t.)

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

Sidebar

Related Questions

I have a NSTimer object that I need to invalidate if a user taps
Here I have a code that sets fireDate of an NSTimer object. -(void)TimerFired:(NSTimer*)timer{ long
I have an NSTimer running in my application that collects some data and sends
I have a NSTimer that I create when entering method - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary
I have created an app that uses NSTimer, which gets triggered each second. My
I have my NSTimer embedded in a class that plays image sequences. Basically it
I have created a NSTimer and call it every 15 seconds. Method that I
I have an NSTimer that fires off every second, and on that second I
I have a NSTimer that fires with an interval of 3 seconds to decrease
I have a NSTimer object. timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; [timer

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.