1

我想从某个服务器下载一些文件。下载文件在 Thread 中运行的功能。所以,你可以在这里看到:

    NSURL *url = [NSURL URLWithString:fileDataBaseURL];
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
                                          cachePolicy:NSURLRequestUseProtocolCachePolicy
                                      timeoutInterval:60.0];

NSURLConnection *theConnection = [NSURLConnection connectionWithRequest:theRequest delegate:self];
if (theConnection) {
    [[NSFileManager defaultManager] createFileAtPath:fileDataBaseEnc contents:nil attributes:nil];
    self._handleDataBaseEnc = [NSFileHandle fileHandleForUpdatingAtPath:fileDataBaseEnc];
} else {
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

}

我也有功能:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [_handleDataBaseEnc seekToEndOfFile];

    NSMutableData *receivedData = [NSMutableData data];
    [receivedData setLength:0];
    [receivedData appendData:data];

    [_handleDataBaseEnc writeData:receivedData];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    [_handleDataBaseEnc closeFile];
    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
}

 - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
 {
     [_handleDataBaseEnc closeFile];
     [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
 }

问题

为什么这三个功能不起作用?它不打电话,从来没有。

我该如何解决?

4

2 回答 2

1

从这里的Art的答案:NSURLConnection 委托方法不被调用

尝试在主线程上运行操作:

NSURLConnection * connection = [[NSURLConnection alloc] 
                                initWithRequest:request
                                       delegate:self startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                      forMode:NSDefaultRunLoopMode];
[connection start];
于 2013-01-30T15:16:11.647 回答
0

发布的代码中没有任何内容可以说明为什么不调用委托方法。我注意到他们的一件事是他们没有做任何特别的事情。如何在一行中运行连接:

+ (void)sendAsynchronousRequest:(NSURLRequest *)request queue:(NSOperationQueue *)queue completionHandler:(void (^)(NSURLResponse*, NSData*, NSError*))handler

在实例化连接之后立即调用它。

于 2013-01-30T15:31:02.290 回答