简体   繁体   English

在connectionDidFinishLoading之后收到NSURLConnection数据

[英]NSURLConnection data received after connectionDidFinishLoading

I have a question about NSURLConnection: I want download an image with: 我有关于NSURLConnection的问题:我要下载图像:

NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];

the first delegate called (correctly) is 第一个被正确调用的委托是

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response

after this is called one time: 之后被称为一次:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data

and immediately: 并立即:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

up to here everything is correct, but after connectionDidFinishLoading is fired once again the didReceiveData delegate for the same connection, I identify connection by: 到这里为止,一切都是正确的,但是在再次触发connectionDidFinishLoading后,对于同一连接,didReceiveData委托通过以下方法识别连接:

NSString * connectionKey = [[[connection originalRequest] URL] absoluteString];

Is this possible? 这可能吗?

update, more infos: 更新,更多信息:

My app fire many simultaneous connections and I store infos for any connection in a dictionary, when a delegate was called I retrive connection info using the key: NSString * connectionKey = [[[connection originalRequest] URL] absoluteString]; 我的应用程序触发了多个同时连接,并且在调用代理时,我将任何连接的信息存储在词典中,我使用以下键检索连接信息:NSString * connectionKey = [[[[connection originalRequest] URL] absoluteString]; for 99% of connections everything is all right but for one (avery time the same!) connection I have this behavior 对于99%的连接,一切正常,但是对于一个(通常相同!)连接,我有此行为


here the complete implementation: 这里是完整的实现:

    - (void)downloadFileFromUrl:(NSURL *)url
                     inPath:(NSString *)completeFilePath
          dataReceivedBlock:(void (^)(long long byteDownloaded ,long long totalByte))dataReceivedBlock
                   endBlock:(void (^)(NSString * downloadPath, NSDictionary * responseHeaders))endBlock
                  failBlock:(void (^)(NSString * downloadPath, NSDictionary * responseHeaders, NSError * error))failBlock
{
    //save the connection infos       
NSMutableDictionary * requestData = [[NSMutableDictionary alloc] init];
if(dataReceivedBlock)
[requestData setObject:[dataReceivedBlock copy] forKey:@"dataReceivedBlock"];
if(endBlock)
[requestData setObject:[endBlock copy] forKey:@"endBlock"];
if(failBlock)
[requestData setObject:[failBlock copy] forKey:@"failBlock"];

        [requestData setObject:[NSNumber numberWithBool:YES] forKey:@"usingBlock"];
        [requestData setObject: completeFilePath forKey:@"downloadDestinationPath"];

    //delete the file if already on fs
    if([[NSFileManager defaultManager] fileExistsAtPath: completeFilePath])
        [[NSFileManager defaultManager] removeItemAtPath:completeFilePath error:nil];

    // Create the request.
    NSURLRequest * urlRequest = [NSURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:TIME_OUT];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
    if (!theConnection)
    {
        // Inform the user that the connection failed.
        failBlock(completeFilePath,nil,[NSError errorWithDomain:@"Connection fail" code:0 userInfo:nil]);
    }

    //add connection infos to the requests dictionary
    NSString * connectionKey = [[[theConnection originalRequest] URL] absoluteString];
    [self.requests setObject:requestData forKey:connectionKey];
}

here a delegate example: 这里是一个委托示例:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

{

NSString * connectionKey = [[[connection originalRequest] URL] absoluteString];
NSMutableDictionary * requestData = [self.requests objectForKey:connectionKey];
NSFileHandle *file = [requestData objectForKey:@"fileHandle"];
[file closeFile];

//se la richiesta usa o no block
BOOL usingBlock = [[requestData objectForKey:@"usingBlock"] boolValue];
if(usingBlock)
{
    __block void (^endBlock)(NSString * , NSDictionary *) = [requestData objectForKey:@"endBlock"];
    if(endBlock)
        endBlock([requestData objectForKey:@"downloadDestinationPath"],[requestData objectForKey:@"responseHeaders"]);
}
else
    [self.delegate downloadEnded:[requestData objectForKey:@"responseHeaders"]];


//elimino dati richiesta
[self.requests removeObjectForKey:[NSString stringWithFormat:@"%@",connectionKey]];

//Inibisco standby
[UIApplication sharedApplication].idleTimerDisabled = NO;
}

It's impossible. 不可能。 It's must be different connection callback. 它必须是不同的连接回调。

ensure each connection use unique delegate, or unique switch branch in the same delegate. 确保每个连接使用唯一的委托,或在同一委托中使用唯一的交换机分支。

Obviously the solution is really simple -_- The error is in the connection key identifier: 显然,解决方案非常简单-_-错误在于连接密钥标识符:

NSString * connectionKey = [[[connection originalRequest] URL] absoluteString];

my assumption is that key are unique, but if I make x parallel downloadS for the same file at the same url the assumption is wrong ;) 我的假设是密钥是唯一的,但是如果我对同一文件在相同的url上执行x parallel downloadS,则该假设是错误的;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在NSURLConnection中控制接收数据的顺序? - controlling order of received data in NSURLConnection? 在connectionDidFinishLoading内部创建一个新的NSURLConnection - Creating a new NSURLConnection inside of connectionDidFinishLoading iPhone-为什么NSURLConnection connectionDidFinishLoading没有运行? - iphone - Why the NSURLConnection connectionDidFinishLoading is not running? 发送许多NSURLConnection请求并在connectionDidFinishLoading中获取响应 - send many NSURLConnection requests and get response in connectionDidFinishLoading NSURLConnection和connectionDidFinishLoading在多个函数之间共享 - NSURLConnection and connectionDidFinishLoading sharing between multiple functions NSURLConnection didFailWithError connectionDidFinishLoading同时调用? - NSURLConnection didFailWithError connectionDidFinishLoading called at same time? 连接后返回DidFinishLoading - Going Back after connectionDidFinishLoading iPhone NSURLConnection:connectionDidFinishLoading - 如何将字符串返回给调用方法 - iPhone NSURLConnection: connectionDidFinishLoading - how to return a string to the calling method connectionDidFinishLoading后返回collectionView函数 - Return to collectionView function after connectionDidFinishLoading NSURLConnection。 我需要帮助来保存收到的数据 - NSURLConnection. I need a help saving data received
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM