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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:47:58+00:00 2026-06-13T23:47:58+00:00

I have a background method that is called every 5 minutes. That method use

  • 0

I have a background method that is called every 5 minutes.

That method use NSURLConnection to send data to web service.
In that method I have this code:

...
conn =  [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO];
    NSPort* port = [NSPort port];
    NSRunLoop* rl = [NSRunLoop currentRunLoop];
    [rl addPort:port forMode:NSDefaultRunLoopMode];
    [conn scheduleInRunLoop:rl forMode:NSDefaultRunLoopMode];
    [conn start];
    [rl run];
...

Because I call WS in background I use this code because without this delegate methods (didReceiveData, didFail… etc.) never get called.

I get this trick from http://www.cocoaintheshell.com/2011/04/nsurlconnection-synchronous-asynchronous/

The issue is when 5 minutes pass and this method is called for the first time it doesn’t wait for next 5 minutes to call again but call web service almost every second.
I think that I should somehow invalidate this NSRunLoop but don’t know how and where?

  • 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-13T23:47:59+00:00Added an answer on June 13, 2026 at 11:47 pm

    In case you were wondering why the delegate methods don’t get called while your connection is in the background: If you put the connection in a background queue, it gets shoved away after the queue is complete, and thus you don’t get your delegate callbacks. The connection has to be in the main queue so it stays in the run loop for callbacks to occur. Or, in your case, create it’s own run loop.

    If you don’t want to deal with runloops, all you have to do is put the connection on the main queue:

    dispatch_async(dispatch_get_main_queue(), ^(void){
        conn =  [[NSURLConnection alloc] initWithRequest:req delegate:self startImmediately:NO];
        [conn start];
    }
    

    Then you should get your delegate callbacks.

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

Sidebar

Related Questions

I have a background service that is getting updates from the location manager every
I have this background image that is 175x175 but I am trying to make
i have a background service on my android APP that is getting my GPS
I have a method which is called in a loop, that looks something like
I have a background thread (NSOperation) that I use to get new messages for
I have a function on a background thread that looks like this: NSMutableArray *descriptions
I have a background thread and the thread calls some methods that update the
I have a method what I want to call after -viewDidLoad and in background
I have a background service in which I want to show a notification which
I want to use Core Data in background threads and NSOperationQueue. Now I thought

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.