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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:16:29+00:00 2026-06-14T23:16:29+00:00

I am using NSTimer in Objective-C to run a background process periodically (1 min

  • 0

I am using NSTimer in Objective-C to run a background process periodically (1 min interval) which checks for any unsaved data and submits to database through a web service. This process runs fine without blocking the main application’s flow until one particular scenario when the web server was re-started.

When the web server was coming up the background process was trying to connect to the server and just hung up there until the server was fully started and reacheable. When it got hung up it also blocked the main applications flow and freezed the entire screen. When the webserver was fully started the hung up process resumed and the main application also resumed working.

Can anyone please let me know why the main application was intermittently freezed when a background process was hung up.

Following code start the timer

- (void) startSync{
    syncTimer = [NSTimer scheduledTimerWithTimeInterval:60
                                         target:self
                                       selector:@selector(syncExtSys:)
                                       userInfo:nil
                                        repeats:YES];
}

Following method is called every 60 seconds and saveAgendaToDb method call calls the web service:

- (void) syncExtSys:(NSTimer *) theTimer{
    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:[NSBundle mainBundle]];
    AgendaItemViewController *agendaItemViewController = [storyboard instantiateViewControllerWithIdentifier:@"AgendaItemViewController"];
    RollCallViewController *rollCallViewController = [storyboard instantiateViewControllerWithIdentifier:@"RollCallViewController"];

    for(int i=0;i<agenda.count;i++){
        unsavedAgenda = [agenda objectAtIndex:i];
        if(unsavedAgenda !=nil && [unsavedAgenda.dirtyFlag isEqualToString:@"Y"] && [self checkNetworkStatus]){

            NSLog(@"agenda out of sync = %@ %@",unsavedAgenda.measureType,unsavedAgenda.measureNum);
            [agendaItemViewController saveAgendaToDb:[agendaItemViewController createDictForAgenda:unsavedAgenda]:unsavedAgenda];
        }
    }
    for(int i=0;i<_rollCall.count;i++){
        Member *member = [_rollCall objectAtIndex:i];

        if(member !=nil && [member.dirtyFlag isEqualToString:@"Y"] && [self checkNetworkStatus]){

            [rollCallViewController saveRollCall:[rollCallViewController createRollCall]];
            break;
        }
    }

    [self calcDirtyFlag];
}

Code calling the web service

-(void) saveAgendaToDb:(NSMutableDictionary*) agendaDictionary:(AgendaItem*) agenda{
    NSError *error;
    CommVotesAppDelegate *delegate = (CommVotesAppDelegate *)[[UIApplication sharedApplication] delegate];
    @try{

        NSString *postUrlString = [NSString stringWithFormat:@"%@%@:%@%@%@/%@",@"http://",delegate.prop.serverName,
                                   delegate.prop.port,delegate.prop.urlPattern,delegate.prop.saveAgendaMethod,delegate.prop.objectType];
        NSURL *postUrl = [NSURL URLWithString:postUrlString];
        NSMutableURLRequest *urlRequestPost = [[NSMutableURLRequest alloc] initWithURL:postUrl];
        NSData *postData = [NSJSONSerialization dataWithJSONObject:agendaDictionary options:0 error:&error];
        [urlRequestPost setHTTPMethod:@"POST"];
        [urlRequestPost setValue:@"text/plain" forHTTPHeaderField:@"Accept"];
        [urlRequestPost setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
        [urlRequestPost setHTTPBody: postData];
        // NSLog(@"After calling webservices");
        NSData *returnData = [ NSURLConnection sendSynchronousRequest: urlRequestPost returningResponse: nil error: &error ];
        NSString *returnString = [[NSString alloc] initWithData:returnData encoding: NSUTF8StringEncoding];
        NSLog(@"Response after calling save agenda web service = %@",returnString);
       [delegate dirtyFlagCalc:returnString :[error localizedDescription]:agenda];
    }@catch(NSException *nserror){
      NSLog(@"Error while sending agenda through webservices = %@",nserror.debugDescription);
    }@finally {
        NSLog(@"Inside finally block of agenda saving ");
        [delegate saveDataToDisk];
    }
}
  • 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-14T23:16:30+00:00Added an answer on June 14, 2026 at 11:16 pm

    The timer fires on the main thread. If the syncExtSys: method does not complete promptly it will hang the main thread.

    Your situation is that you want to do something that may potentially take awhile but you want the results to be displayed in the UI on the main thread. Some good ways to handle this is to do one of the following with Grand Central Dispatch:

    1. Use a timer dispatch source to do the processing on a background queue and then dispatch the results for display on the main queue.
    2. Stick with the NSTimer but dispatch anything that may potentially take awhile in syncExtSys: to a background queue and then dispatch the results back to the main queue on completion.
    3. Use all non-blocking calls in syncExtSys: and everything that it calls.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created an animated game app, In which I am using NSTimer to
I'm using a NSTimer to run an animation (for now just call it myMethod
I am using an NSTimer which I have working to show minutes and seconds.
I'm using NSTimer in my program to play video, NSTimer run continuously to run
Using NSTimer for my application in which I have to show countdown timer with
How to change images periodically using NSTimer in iPhone progranmming? I create an image
I recently switched from using NSTimer to CVDisplayLink to redraw my OpenGL animation, but
I am currently making a application using NSTimer as base for recording time. However,
I would like to set time count down, using NSTimer like start with 20
I am using NSThread along with NSTimer. My code is like this -(void) checkForRecentAlarm

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.