【问题标题】:NSURLDownload not startingNSURLDownload 没有开始
【发布时间】:2010-02-22 06:44:10
【问题描述】:

我正在尝试使用 NSURLDownload 下载 URL,但它没有开始下载。在继续之前,必须说我正在为此使用 GNUStep。

我的项目大纲如下:

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];
}

我在 MyClass 中的两个函数上都有 NSLog,但它们都没有被命中。我必须做什么才能开始下载?或者这是 GNUStep 的问题?

【问题讨论】:

  • 您确定要继承 Object 而不是 NSObject?您可能应该使用 NSObject,以防万一某些东西将您的委托放入集合或其他东西中。 (另外,你真的应该想一个比“MyClass”更好的名字,并通过遵循内存管理规则来适当地管理对象的生命周期:developer.apple.com/mac/library/documentation/General/…

标签: objective-c cocoa gnustep


【解决方案1】:

NSURLDownload 在后台下载,所以对initWithRequest:delegate: 的调用会立即返回。

除非您的程序将控制权交给运行循环(这对于应用程序来说是自动处理的,但对于工具来说必须手动完成),它只会执行 main() 函数的其余部分并终止。

此外,发送给您的委托的消息是从运行循环中发送的,因此即使 main() 没有立即退出,您的委托仍然不会收到 downloadDidBegin:downloadDidFinish:,除非您的代码第一次调用NSRunLoop 的运行方法。

将以下行添加到您的代码中,紧接在[pool drain]; 之前:

[[NSRunLoop currentRunLoop] run];

有关运行循环的更多信息,请查看Thread Programming Guide

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-01
    • 2018-12-02
    • 2021-08-14
    • 1970-01-01
    相关资源
    最近更新 更多