NSURLConection在NSOperation中不起作用

时间:2014-05-22 12:35:42

标签: nsurlconnection nsoperation

我正在尝试使用NSOperation中的异步UrlConnection下载图像,但Urlconection委托不起作用。如果我尝试同步下载它可以工作。

- (void)main
{
    if (self.isCancelled)
    {
        NSLog(@"isCancelled");
        return;
    }

    NSMutableURLRequest * req = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [req setTimeoutInterval:10.0];
    connection = [NSURLConnection connectionWithRequest:req delegate:self];
    [connection start];

    if (self.isCancelled)
    {
        NSLog(@"isCancelled");
        return;
    }
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error;
{

}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response;
{
    mutableData = [NSMutableData data];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data_;
{
    NSLog(@"data_");
    [mutableData appendData:data_];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;
{
    self.callBack(mutableData, self);
}

0 个答案:

没有答案