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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T17:57:53+00:00 2026-06-08T17:57:53+00:00

I have a -(void) method being executed and at one point it gets to

  • 0

I have a -(void) method being executed and at one point it gets to a while loop that gets values from the accelerometer directly the moment it asks for them

I went throughout the documentation regarding the NSTimer class but I couldn’t make sense of how exactly I am to use this object in my case :

e.g.

-(void) play
{
    ......
    ...



    if(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9 )
    {
        startTimer;
    }

    while(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9)
    {
        if(checkTimer >= 300msec)
        {

           printOut_AccelerationStayedBetweenThoseTwoValuesForAtLeast300msecs;
           break_Out_Of_This_Loop;
        }

    }

    stopTimerAndReSetTimerToZero;

    .....
    more code...
    ....
    ...
}

Any help?

  • 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-08T17:57:54+00:00Added an answer on June 8, 2026 at 5:57 pm

    You cannot do it with NSTimer, because it needs your code to exit in order to fire. NSTimer uses the event loop to decide when to call you back; if your program holds the control in its while loop, there is no way for the timer to fire, because the code that checks if it’s time to fire or not is never reached.

    On top of that, staying in a busy loop for nearly a second and a half is going to deplete your battery. If you simply need to wait for 1.4s, you are better off calling sleepForTimeInterval:, like this:

    [NSThread sleepForTimeInterval:1.4];
    

    You can also use clock() from <time.h> to measure short time intervals, like this:

    clock_t start = clock();
    clock_t end = start + (3*CLOCKS_PER_SEC)/10; // 300 ms == 3/10 s
    while(accelerationOnYaxis >0 && accelerationOnYaxis < 0.9)
    {
        if(clock() >= end)
        {
           printOut_AccelerationStayedBetweenThoseTwoValuesForAtLeast300msecs;
           break_Out_Of_This_Loop;
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that is being executed on a background thread. From that
Assume I have one threadpool and each thread is running following method: void runMe(HashMap
I have a method that contains an asynchronous call like this: public void MyMethod()
Imagine I have a method: void Method(bool parameter){ if(parameter){ // first case } else
I have a method -(void)myMethod:(MyObject*)obj And I am detaching a new thread [NSThread detachNewThreadSelector:@selector(myMethod)
I currently have this method: -(void)seperateGuides { DLog(GetGuideListCount: %i, [[appDelegate getGuideList] count]); columnOneArray =
I have a UISearchbar with delegate method - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText { if(searchText.length>3){
I have a private variable in my class: BOOL isEnabled; and a method: -(void)
I have a method: public void StoreNumberInSmallestType(ValueType number) { if (numberTypes == null) numberTypes
I have this method: public void testJSNI2(){ String x = test; } I can

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.