gpt4 book ai didi

nsurlconnection - 取消 NSURLConnection 以 sendAsynchronousRequest :queue:completionHandler:? 开始

转载 作者:行者123 更新时间:2023-12-04 01:10:39 26 4
gpt4 key购买 nike

  • 是否可以取消 NSURLConnection开始于 sendAsynchronousRequest:queue:completionHandler: ?
  • 为什么不sendAsynchronousRequest:queue:completionHandler:返回 NSURLConnection它创建的对象以便我可以取消它?
  • 最佳答案

    编辑 - 由于我提供的答案似乎有误,您应该查看以下相关问题及其答案:

    How can I cancel an asynchronous call through NSURLConnection sendAsynchronousRequest?

    旧答案:

    在类的标题(例如 ViewController)中,声明一个操作队列:

    NSOperationQueue *downloadOperationQueue;

    要下载文件,请调用如下所示的内容:
    downloadOperationQueue = [[NSOperationQueue alloc] init];
    NSURLRequest *fileRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.google.com/images/nav_logo114.png"]
    cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
    timeoutInterval:10];
    [NSURLConnection sendAsynchronousRequest:fileRequest
    queue:downloadOperationQueue
    completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSLog(@"download successful");
    }];

    稍后在通话中取消下载:
    [downloadOperationQueue cancelAllOperations];

    关于nsurlconnection - 取消 NSURLConnection 以 sendAsynchronousRequest :queue:completionHandler:? 开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10790355/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com