gpt4 book ai didi

ios - 如何取消 [NSURLConnection sendAsynchronousRequest :. ] 发送的请求。]

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:00:39 27 4
gpt4 key购买 nike

我用这段代码发出请求:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
// Here I want to cancel Request , Request Url contains searchBar Texts
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSXMLParser *p = [[NSXMLParser alloc] initWithData:reqData];
[p setDelegate:self];
[p parse];
}];
}

最佳答案

你可以声明

@property (nonatomic, strong) NSOperationQueue *downloadQ;

然后,

    self.downloadQ = [NSOperationQueue new];
self.downloadQ.name = @"download";

[NSURLConnection sendAsynchronousRequest:request queue:self.downloadQ completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSXMLParser *p = [[NSXMLParser alloc] initWithData:reqData];
[p setDelegate:self];
[p parse];
}];

如果你需要停止

NSOperation *lastOp = [self.downloadQ.operations lastObject];
[lastOp cancel];

更新:

现在,最好使用 NSURLSession,它有合适的方法:

- (void)getTasksWithCompletionHandler:(void (^)(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks))completionHandler;

然后检查返回任务的 task.originalRequest.URL 以找到您要取消的任务。

关于ios - 如何取消 [NSURLConnection sendAsynchronousRequest :. ] 发送的请求。],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31088865/

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