如何手动强制关闭NSURLConnection?

时间:2012-11-29 10:24:52

标签: iphone xcode cocoa

我正在使用NSURLConnection进行http下载。根据具体情况,我可能必须在特定下载后断开连接。比方说,如果我的下载在30秒内完成,那么其他明智的下载应该在30秒后停止。此外,我必须记录这些事件。我的问题是,即使在30秒后它仍然继续下载数据,并且只有在下载完成后才会记录事件。

简单来说,我想强制关闭下载,并且还要禁用http连接触发的所有委托事件。我不想在多个位置设置标志,这会使事情进一步复杂化。任何想法?

更新:完成方案: 我在NSURLReqeust对象中将timeoutInterval设置为10秒。现在如果10秒内没有收到任何数据,连接会在10秒后自动丢失,会发生什么情况。但我的软件中还有另一项功能,即在给定的时间内未完成下载终止连接。我正在使用单独的NSTimer。我所能做的就是在触发NSTimer事件时设置一个标志。现在,如果通过NSTimer设置标志并且数据停止进入,我没有将在接下来的10秒内触发的连接委托。现在我的问题是中止事件和超时事件同时发生。

2 个答案:

答案 0 :(得分:2)

好吧,你可以通过发送取消事件取消NSURLConnection:

[connection cancel];

请参阅Apple docs

在此之前,只需要委托代表,并且不应该受到任何委托回调的骚扰。

答案 1 :(得分:1)

使用NSURLRequest对象为使用此requestWithURL:cachePolicy:timeoutInterval:方法下载的evrey请求指定超时。

请检查您的NSURLConnection代表是否已设置并响应connection:didFailWithError:方法。 NSURLConnection在连接完成时调用此方法或connectionDidFinishLoading:

处理'didFailWithError'方法并使用NSError对象检查failer的原因。

但是如果你从服务器得到响应并且响应时间很慢,则使用NSTimer。 创建Helper类以下载数据,这样您就可以通过创建多个实例并在其中设置NSTimer来重复使用该类进行多次下载,如果下载在30秒内完成,则无效的计时器取消下载[self.connection cancel]

请检查以下代码:

- (void)_startReceive
    // Starts a connection to download the current URL.
{
    BOOL                success;
    NSURL *             url;
    NSURLRequest *      request;

        // Open a connection for the URL.
        request = [NSURLRequest requestWithURL:url];
        assert(request != nil);

        self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
        assert(self.connection != nil);

        // If we've been told to use an early timeout for get complete response within 30 sec, 
        // set that up now.
            self.earlyTimeoutTimer = [NSTimer scheduledTimerWithTimeInterval:30.0 target:self selector:@selector(_earlyTimeout:) userInfo:nil repeats:NO];
    }
}

- (void)_stopReceiveWithStatus:(NSString *)statusString
    // Shuts down the connection and displays the result (statusString == nil) 
    // or the error status (otherwise).
{
    if (self.earlyTimeoutTimer != nil) {
        [self.earlyTimeoutTimer invalidate];
        self.earlyTimeoutTimer = nil;
    }
    if (self.connection != nil) {
        [self.connection cancel];
        self.connection = nil;
    }
}

- (void)_earlyTimeout:(NSTimer *)timer
    // Called by a timer (if the download is not finish)
{
    [self _stopReceiveWithStatus:@"Early Timeout"];
}

- (void)connection:(NSURLConnection *)conn didReceiveResponse:(NSURLResponse *)response
    // A delegate method called by the NSURLConnection when the request/response 
    // exchange is complete.  
{ }

- (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
    // A delegate method called by the NSURLConnection as data arrives.  We just 
    // write the data to the file.
{ }

- (void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error
    // A delegate method called by the NSURLConnection if the connection fails. 
{
    NSLog(@"didFailWithError %@", error);   
    // stop Receive With Status Connection failed
}

- (void)connectionDidFinishLoading:(NSURLConnection *)conn
    // A delegate method called by the NSURLConnection when the connection has been 
    // done successfully.  We shut down the connection with a nil status.
{
    NSLog(@"connectionDidFinishLoading");
    // If control reach here before timer invalidate then save the data and invalidate the timer
     if (self.earlyTimeoutTimer != nil) {
        [self.earlyTimeoutTimer invalidate];
        self.earlyTimeoutTimer = nil;
    }
}