当我使用 NSURLRequest 发出 POST 请求时,为什么我的应用程序会冻结?
我有一个这样的方法。当我的设备通过 wifi 网络连接时它工作正常,但当它通过 3G 网络连接时它会冻结我的应用程序几秒钟。 因此,因为它是一个交互式应用程序,所以当它执行一些各种发布请求时,它必须继续运行,以便用户可以继续使用该应用程序。 有什么解决办法吗?
我尝试减少 [theRequest setTimeoutInterval:2.0];但这并没有解决我的问题。
// post request
- (void)postRequestWithURL:(NSString *)url
body:(NSString *)body
contentType:(NSString *)contentType
options:(NSDictionary *)dict
{
// set request
NSURL *requestURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
if([dict count] > 0)
{
for (id key in dict) {
NSLog(@"[theRequest addValue:%@ forHTTPHeaderField:%@]", [dict valueForKey:key], key);
[theRequest addValue:[dict valueForKey:key] forHTTPHeaderField:key];
}
}
if (contentType != nil) {
[theRequest addValue:contentType forHTTPHeaderField:@"Content-type"];
}
[theRequest setURL:requestURL];
[theRequest setTimeoutInterval:2.0];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding]];
[self.oauthAuthentication authorizeRequest:theRequest];
// make request
//responseData = [NSURLConnection sendSynchronousRequest:theRequest
// returningResponse:&response
// error:&error];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
self.web = conn;
[conn release];
NSLog(@"########## REQUEST URL: %@", url);
// request and response sending and returning objects
}
I have a method like this. It works fine when my device is connected through wifi network, but when it is connected through 3G network it freezes my app for some seconds.
So, because it is an interactive app, when it does some various post request it must continuing running so the user can continue to use the app.
Any solution for this ?
I tried reducing the [theRequest setTimeoutInterval:2.0]; but that didn't fix my problem.
// post request
- (void)postRequestWithURL:(NSString *)url
body:(NSString *)body
contentType:(NSString *)contentType
options:(NSDictionary *)dict
{
// set request
NSURL *requestURL = [NSURL URLWithString:url];
NSMutableURLRequest *theRequest = [[NSMutableURLRequest alloc] init];
if([dict count] > 0)
{
for (id key in dict) {
NSLog(@"[theRequest addValue:%@ forHTTPHeaderField:%@]", [dict valueForKey:key], key);
[theRequest addValue:[dict valueForKey:key] forHTTPHeaderField:key];
}
}
if (contentType != nil) {
[theRequest addValue:contentType forHTTPHeaderField:@"Content-type"];
}
[theRequest setURL:requestURL];
[theRequest setTimeoutInterval:2.0];
[theRequest setHTTPMethod:@"POST"];
[theRequest setHTTPBody:[body dataUsingEncoding:NSASCIIStringEncoding]];
[self.oauthAuthentication authorizeRequest:theRequest];
// make request
//responseData = [NSURLConnection sendSynchronousRequest:theRequest
// returningResponse:&response
// error:&error];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
self.web = conn;
[conn release];
NSLog(@"########## REQUEST URL: %@", url);
// request and response sending and returning objects
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它正在冻结您的应用程序,因为您已经告诉它了。您已将 YES 传递给 startImmediately。这意味着它将在该线程上启动连接并等待它完成。我猜你正在执行此操作的线程将是主线程 - 还处理 ui 等的线程:)
你需要使用类似
connectionWithRequest:delegate:
- 这将在以下位置运行请求背景并告诉您何时完成。PS 你没有发现 wifi 中的错误的原因是因为数据发送得太快,你无法注意到应用程序中的暂停:)
PPS 超时没有修复它的原因是因为请求没有计时出 - 它只是非常缓慢地获取数据:)
编辑
如下:
It's freezing your app because you have told it to. You have passed YES into startImmediately. This means that it will start the connection on that thread and wait until it's finished. I guess the thread you're doing this on will be the main thread - the one that also handles the ui etc :)
You need to use something like
connectionWithRequest:delegate:
- this will run the request in the background and tell you when it's done.PS The reason you didn't spot the bug in wifi is because the data was sent so fast you couldn't notice the pause in your app :)
PPS The reason the timeout didn't fix it is because the request wasn't timing out - it was just getting data very slowly :)
EDIT
Something like this :