在connectionDidFinishLoading之后收到的NSURLConnection数据

时间:2013-02-21 10:47:16

标签: iphone ios objective-c nsurlconnection nsurlconnectiondelegate

我对NSURLConnection有疑问: 我想下载一张图片:

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

第一个被叫(正确)的委托是

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

在此之后被召唤一次:

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

立即:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection

到目前为止一切正常,但在再次触发connectionDidFinishLoading后,同一连接的didReceiveData委托,我通过以下方式识别连接:

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

这可能吗?

更新,更多信息:

我的应用程序触发许多同时连接,并且我为字典中的任何连接存储信息,当调用委托时,我使用密钥检索连接信息:NSString * connectionKey = [[[original originalRequest] URL] absoluteString];对于99%的连接,一切都可以,但对于一个(每次都相同!)连接,我有这种行为


这里是完整的实施:

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

这是一个委托示例:

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

2 个答案:

答案 0 :(得分:0)

这是不可能的。它必须是不同的连接回调。

确保每个连接在同一个委托中使用唯一委托或唯一开关分支。

答案 1 :(得分:0)

显然解决方案非常简单-_- 错误在连接密钥标识符中:

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

我的假设是密钥是唯一的,但如果我在同一个url为同一个文件进行x并行下载,则假设是错误的;)