1

I use a NSThread in order to download videos and images from a server side.It work looks and works great except the fact that when the downloading is done my GUI gets blocked until the download is complete.When the download is finished it takes a few seconds to work again.

this is how the server request is done:

- (void) repeatRequest{
    NSLog(@"repeatRequest");
    [NSThread detachNewThreadSelector:@selector(backgroundRequest) toTarget:self withObject:nil];
}

- (void) backgroundRequest{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSURL *url = [NSURL URLWithString:myURLStr];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setDelegate:self];
    [request startAsynchronous];

    [pool drain];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
    //do things
}

IMPORTANTAnd I also tried to start the ASIHTTPRequest from the GUI thread but with the same behaviour.

Any idea about what could be wrong?

EDIT:

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];

    [[UIApplication sharedApplication] setStatusBarHidden:YES];

    //internetReachable = [[Reachability reachabilityForInternetConnection] retain];

    if(timer1 == nil)
    {
        timer1 = [NSTimer scheduledTimerWithTimeInterval:60.0 target:self selector: @selector(repeatRequest) userInfo: nil repeats: YES];
    }

}
adrian
  • 4,574
  • 17
  • 68
  • 119

2 Answers2

2

Try to run synchronous ASIHTTPRequest in your background thread, and handle results not in delegate method (requestFinished), but after [request startSynchronous];

  • and handle results not in delegate method (requestFinished), but after [request startSynchronous]; how to do this?Please tell...:) – adrian Nov 21 '11 at 12:44
  • Replase `[request startAsynchronous];` with `[request startSynchronous];` and after `[request startSynchronous];` line do things what you want to do in `- (void)requestFinished:(ASIHTTPRequest *)request` – Aliaksandr Andrashuk Nov 21 '11 at 12:48
  • should I delete the request finished method?And also delete this line:[request setDelegate:self];???Thank you:) – adrian Nov 21 '11 at 12:50
  • ok it is working.But please telll me what is the explanation of this?Why is different from what I did.Thank you – adrian Nov 21 '11 at 12:55
  • You can remove `[request setDelegate:self]` – Aliaksandr Andrashuk Nov 21 '11 at 14:04
  • Explanation: you want to run `backgroundRequest` method in background, then you run `ASIHTTPRequest` in background too, this is not right. This way you got 2 background threads. – Aliaksandr Andrashuk Nov 21 '11 at 14:06
-1

I don't know anything about ASIHTTPRequest but i would assume its -startAsynchronous method already handles the background downloading for you. It all likelihood, it is returning immediately and your new thread is exiting. Also, you should just use [pool release] at the end of a thread method instead of [pool drain], it will be drained upon release, and you won't be leaking an NSAutoReleasePool. Does ASIHTTPRequest have a -startSynchronous (or just plain -start) method? Try using that within -backgroundRequest, as it should block the premature exit of that thread.

Nicholas Shanks
  • 10,623
  • 4
  • 56
  • 80
  • 1
    This is wrong. -drain "releases and pops the receiver". http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAutoreleasePool_Class/Reference/Reference.html –  Nov 21 '11 at 12:39
  • Well I used -startSynchronous method with the thread and it has trhe same behaviour.And as I said in the IMPORTANT part of the question...i already tried to start the request asynchronous from the GUI thread but stilll not working! – adrian Nov 21 '11 at 12:43