Ios NSURLConnection和NSURLConnectionDataDelegate:未触发连接回调

Ios NSURLConnection和NSURLConnectionDataDelegate:未触发连接回调,ios,objective-c,nsurlconnectiondelegate,Ios,Objective C,Nsurlconnectiondelegate,我想实现从服务器下载文件的过程。 我使用的代码是一个自定义类,由 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://example.com"]]; if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) { DownloadCallback

我想实现从服务器下载文件的过程。 我使用的代码是一个自定义类,由

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://example.com"]];
            if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6.0")) {
                DownloadCallback *dc = [[DownloadCallback alloc] initWithCallbackProgress:^(long long res){
                    NSLog(@"%lld", res);
                } withCallbackReady:^(long long res){
                    NSLog(@"READY %lld", res);
                    [[NSOperationQueue mainQueue] addOperationWithBlock:^{

                    }];

                } withCallbackError:^(NSError * error) {
                    NSLog(@"READY %@", error.domain);
                }];

                NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:dc];
//                [connection setDelegateQueue:[[NSOperationQueue alloc] init]];
                [connection start];
标题:

@interface DownloadCallback: NSObject<NSURLConnectionDataDelegate>{
     @private void (^_progressHandler)(long long someParameter);
     @private void (^_readyHandler)(long long someParameter);
     @private void (^_errorHandler)(NSError *someParameter);
}
-(id) initWithCallbackProgress:(void(^)(long long))handler withCallbackReady:(void(^)(long long))handlerReady  withCallbackError:(void(^)(NSError*))handlerError;
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
@end
但是回调事件不会被触发!一点也不

简单的同步代码可以完美地工作:

    // Send a synchronous request
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://example.com"]];
    NSURLResponse * response = nil;
    NSError * error = nil;
    NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest
                                          returningResponse:&response
                                                      error:&error];
    if (error == nil) {
        // Parse data here
    }
但我需要回电话!如何解决?我在stackoverflow中找不到解决方案。
此外,如果我使用对主类的简单委托而不是
DownloadCallback
,则相同:连接回调也不会触发。

将dealloc方法添加到回调类中,并在其中输出断点或log语句。查看是否在调用回调之前解除分配

如果是这种情况,您的回调类实例将很快被销毁。使其成为一个类的属性,该类肯定会比请求的寿命更长

此外,您应该确保以下代码:

NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:dc];
[connection start];
在一个线程上调用,该线程在连接之外,并且有一个runloop。实现这一点的最简单方法是在主队列上调用该代码。您的代码示例没有显示调用的队列。如果它不起作用,我想这是因为你在后台队列中调用它。 您可以从想要/需要的代理回调中分派到后台队列


作为旁注,如果您正在构建新的东西,您应该尝试使用NSURLSession而不是NSURLConnection。NSURLSession更安全、更易于使用且不被弃用。NSURLConnection已被弃用。

遗憾的是,这并不是原因:(您的
NSURLConnection*连接如何?
?能否显示您显示的第一段代码的封闭范围?如果您在
[连接开始]之后没有保持对连接的强引用)
,它将在您调用
start
后立即解除分配。我发现问题是关于主线程执行的-在主线程完成之前,无法执行
NSURLConnection
。因此,我不明白如何在
[[NSOperationQueue alloc]init]内启动NSURLConnection
正确。
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:dc];
[connection start];