iphone - 不要调用连接 :(NSURLConnection *)connection didReceiveData:(NSData *)data

标签 iphone ios objective-c nsurlconnection

我想从某个服务器下载一些文件。下载文件的功能在 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];
 }

问题

为什么这三个功能不起作用?它不会调用,永远不会。

我该如何解决?

最佳答案

来自 Art 的回答:NSURLConnection delegate methods are not called

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

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

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                      forMode:NSDefaultRunLoopMode];
[connection start];

关于iphone - 不要调用连接 :(NSURLConnection *)connection didReceiveData:(NSData *)data,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14607116/

相关文章:

iphone - 克隆现有的球 Sprite 和 box2d cocos2d 中的对象

ios - 如何通过 xcode 对边距约束应用乘数?

ios - 无法使用 Objective-C 调用号码

objective-c - 是否可以在 ios 中添加注释但不更改 map View 区域

ios - 如何在更新 iphone 应用程序时保留库目录中的用户数据?

javascript - 在移动网站上显示谷歌地图

ios - 使用 UISearchResults 过滤时出现问题

ios - 如何为 iOS 的 UIWebView 中加载的文档(doc、html)创建缩略图

ios - 如果我在使用 skobbler 导航时缩小,如何停止自动放大

objective-c - 将方法作为 SEL 参数传递给另一个类方法