gpt4 book ai didi

ios - NSURLConnection 在重新启动应用程序后总是失败

转载 作者:行者123 更新时间:2023-11-28 20:33:27 26 4
gpt4 key购买 nike

我意识到这是一个模糊的问题,但我想知道是否有其他人观察到这一点。这是我调用 NSURLConnection 的代码

   // Get data from server
NSString *host = @"www.hostname.com";
NSString *urlString = [NSString stringWithFormat:@"/theRestOfTheURL"];
NSURL *url = [[NSURL alloc] initWithScheme:@"http" host:host path:urlString];

DLog(@"URL is %@", url);

// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
receivedData_ = [[NSMutableData data] retain];

NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:15.0];

// create the connection with the request
// and start loading the data
self.powerPlantDataConnection = [[[NSURLConnection alloc] initWithRequest:theRequest delegate:self] autorelease];

[url release];

当我第一次加载应用程序时,它工作正常,我可以毫无问题地重复调用它。但是如果我关闭应用程序并重新打开它,

(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

每次都会调用委托(delegate)方法,并显示请求超时错误消息。我有两个不同的 View Controller ,我在其中调用两个不同的 URL,每次关闭并重新打开应用程序后,它们都会失败。

谁能想出发生这种情况的任何原因?我不确定从哪里开始寻找。请求超时错误的原因可能是什么?该请求应该没有问题,因为它在我第一次运行该应用程序时有效。


编辑补充说似乎我只在我的设备上有这个问题,而不是在模拟器上。

最佳答案

希望你分享了一些 chrash 日志(尤其是像这样的清晰定义[error localizedDescription] 类方法..)

如您所说,它将超时(您的请求)。并且由于您创建对象的方式过于困惑,因此您的系统工作量更大。我建议使用 GCD下载数据时,尤其是在像您这样的情况下,具有不同的界面和 url..

一个建议您可以像这样创建您的 url 对象:

   NSURL *url = [NSURL urlWithString:[NSString stringWithFormat:@"http://%@/%@?key1=%@&key2=%@", yourDomain, targetFile, value1, value2]];

关于ios - NSURLConnection 在重新启动应用程序后总是失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11434691/

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