views:

60

answers:

1

I am attempting to download a URL using NSURLDownload, but it does not start downloading. Before continuing it must be said that I am using GNUStep for this.

The outline of my project is as follows:

MyClass.h:

@interface MyClass : Object {

}

-(void)downloadDidBegin:(NSURLDownload*)download;
-(void)downloadDidFinish:(NSURLDownload*)download;

@end

Main.m

int main()
{
   NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];

   NSLog(@"creating url");
   NSURL* url = [[[NSURL alloc] initWithString:@"http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/NSURLRequest_Class.pdf"] autorelease];

   NSLog(@"creating url request");
   NSURLRequest* url_request = [[[NSURLRequest alloc] initWithURL:url] autorelease];

   NSLog(@"creating MyClass instance");
   MyClass* my_class = [[MyClass alloc] init];

   NSLog(@"creating url download");
   NSURLDownload* url_download = [[[NSURLDownload alloc] initWithRequest:url_request
                                                                delegate:my_class] autorelease];

   [pool drain];
}

I have NSLog's on both functions in MyClass and neither of them is hit. What do I have to do to get the download started? Or is this an issue with GNUStep?

+2  A: 

NSURLDownload downloads in the background, so the call to initWithRequest:delegate: returns immediately.

Unless your program passes control to the run loop (this is handled automatically for applications, but must be done manually for tools), it will just execute the rest of the main() function and terminate.

Also, the messages to your delegate are sent from within the run loop, so even if main() didn't exit immediately, your delegate still wouldn't receive downloadDidBegin: or downloadDidFinish: unless your code first called one of NSRunLoop's run methods.

Add the following line to your code, immediately before [pool drain];:

[[NSRunLoop currentRunLoop] run];

For more info on run loops, check out the Thread Programming Guide.

tedge