如何在 iPhone 中中止同步 URL 请求

发布于 2024-10-23 19:44:07 字数 839 浏览 1 评论 0原文

我有一个 API 调用。需要2分多钟才能得到服务器的响应。我如何在处理时手动中止它?

这是我的代码:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeoutInterval: 600.0];
[request setHTTPMethod:@"GET"];

NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];

[headers setValue:@"application/json" forKey:@"Content-Type"];
[headers setValue:@"application/json" forKey:@"Accept"];
[headers setValue:val forKey:@"Authorization"];

[request setAllHTTPHeaderFields:headers];   

NSError *Error;
NSURLResponse *response = [[NSURLResponse alloc] init];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&Error];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

如何在处理请求时中止请求。

谢谢

I have an API call. It takes more than 2 minute to get a response from the server. how can i abort it manually while it is processing?

Here is my code:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[request setTimeoutInterval: 600.0];
[request setHTTPMethod:@"GET"];

NSMutableDictionary* headers = [[[NSMutableDictionary alloc] init] autorelease];

[headers setValue:@"application/json" forKey:@"Content-Type"];
[headers setValue:@"application/json" forKey:@"Accept"];
[headers setValue:val forKey:@"Authorization"];

[request setAllHTTPHeaderFields:headers];   

NSError *Error;
NSURLResponse *response = [[NSURLResponse alloc] init];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&Error];

[[NSURLConnection alloc] initWithRequest:request delegate:self];

how can i abort the request while its processing.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

衣神在巴黎 2024-10-30 19:44:07

你不能。如果您希望能够取消,请使用异步连接。

另外,顺便说一句,您的代码中有一些错误:

NSError *Error;
NSURLResponse *response = [[NSURLResponse alloc] init];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&Error];

[[NSURLConnection alloc] initWithRequest:request delegate:self];
  1. 为了更好地衡量,Error 应该初始化为 nil。
  2. 您正在泄漏响应。将其初始化为 nil,sendSynchronousRequest:returningResponse:error: 将填充它(无论如何,如果请求成功)。
  3. [[NSURLConnection alloc] initWithRequest:request delegate:self] 行启动异步连接并释放它,您已经为同一请求创建了同步连接。并且它泄漏了 NSURLConnection 对象。您是否在您的 self 对象上实现了非正式委托协议的必要方法?

You can't. Use asynchronous connections if you want to be able to cancel.

Also, BTW, you have some errors in your code:

NSError *Error;
NSURLResponse *response = [[NSURLResponse alloc] init];

NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&Error];

[[NSURLConnection alloc] initWithRequest:request delegate:self];
  1. For good measure, Error should be initialized to nil.
  2. You are leaking response. Initialize it to nil, and sendSynchronousRequest:returningResponse:error: will fill it in (if the request succeeds, anyway).
  3. The line [[NSURLConnection alloc] initWithRequest:request delegate:self] starts an asynchronous connection and releases it, after you already did a synchronous connection for the same request. And it leaks the NSURLConnection object. Did you even implement the necessary methods for the informal delegate protocol on your self object?
春夜浅 2024-10-30 19:44:07

你不能,因为线程被你的同步请求阻止。

同步加载构建在类提供的异步加载代码之上。当异步(原文如此)加载系统在专门为此加载请求生成的线程上执行 URL 加载时,调用线程将被阻塞。

http://开发人员。 apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

You can't, as the thread is being blocked by your synchronous request.

synchronous load is built on top of the asynchronous loading code made available by the class. The calling thread is blocked while the asynchronous (sic) loading system performs the URL load on a thread spawned specifically for this load request.

http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文