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

  • Home
  • SEARCH
  • 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 8686399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T22:50:18+00:00 2026-06-12T22:50:18+00:00

I have a Download object which handles NSURLConnection . Then I have NSOperation object

  • 0

I have a Download object which handles NSURLConnection.

Then I have NSOperation object (DownloadOperation) which holds Download object as property.

Download object has ability to start/pause/resume/cancel.

This is the main method of DownloadOperation

- (void)main
{
    @autoreleasepool {

        BOOL isDone = NO;

        if (![self isCancelled]) {   
            [_download start]; //Download object start (creates NSURLConnection internally)
        }

        NSDate *distantFuture = [NSDate distantFuture];
        while(!isDone && ![self isCancelled]) {

            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:distantFuture];

            if (!_download.isActive) { //internal state of Download
                isDone = YES;
            }
        }

        [self completeOperation]; //this sets finished and executing flags
    }
}

From outside (from UI), I manipulate with Download object: Start, Pause, Resume, Cancel.
And internally I am changing its state so when Download is finished or canceled, isActive is set to NO and while loop should end.

This works if I start Download and let it finish (in background, NSURLConnection finished and called delegate –connectionDidFinish…)

If I pause/resume Download, download will continue to download and finish (and change its internal state: isActive -> NO).
On pause I cancel NSURLConnection and on resume I create new.

Or if I cancel the download, it will also be inactive (NSURLConnection is canceled).

But here is the problem:

[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:distantFuture]; never returns in those cases (when I cancel NSURLConnection) and my “if” statement is never handled so this DownloadOperation is always considered running and will never exit my NSOperationQueue.

It looks like there is no event that could be fired that would cause that runloop to wake up.

I tried

            [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:0.05]];

and this kind of works but not so smooth and I don’t think it is the best solution.

What I really want to know is, how to force that NSRunLoop to wake up (how to fire some event that will cause it to wake up) and continue my while loop?

  • 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-12T22:50:19+00:00Added an answer on June 12, 2026 at 10:50 pm

    Here’s what I did.

    Good thing is that I have notifications in my app and I know exactly when downloads are changing. So I’ve made an instance variable NSThread currentThread and at the beginning of main I call currentThread = [NSThread currentThread].

    After I receive notification which I know should cause my thread to wake up I call:

    [self performSelector:@selector(wakeUpThread:) onThread:currentThread withObject:nil waitUntilDone:NO];

    - (void)wakeUpThread does nothing, it is empty method, but purpose of this is to wake up thread and cause my run loop to continue.

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

Sidebar

Related Questions

In my .aspx page, I have download button which onclick download the .apk file.
I have some download implementation which allows me to export all data from a
I have an object which I create from the response from a webservice. If
I have a WPF WebBrowser object in my application in which I am trying
I have a collection of picture Objects for which I need to download thumbs
I have an application in which I want to force an image to download,
I have a user control which has a Canvas of height 100 and width
In my application I have a queue download list which consists of progress bars
i have a service which fetches some data from network.i am sending data download
I have a usercontrol which has a couple of textblocks on it <UserControl x:Class=Tester.Messenger

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.