gpt4 book ai didi

objective-c - iOS 代码中多个 URL 检索缓慢

转载 作者:行者123 更新时间:2023-11-29 04:41:10 24 4
gpt4 key购买 nike

我想访问一个 URL 并从此 URL 检索数据,根据检索到的数据,我想从另一个 URL 检索数据

我尝试通过以下方式访问多个 URL:

- (void) showClassRoom:(NSString *)theMessage{

NSString *queryURL =[NSString stringWithFormat:@"http://nobert.cloudfoundry.com/CalOfEvents/tos"];
NSURL *url = [NSURL URLWithString: queryURL];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

NSError *error;
NSURLResponse *response;

NSData *returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];
if(returnData){
NSString *strResult = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSDictionary *result = [strResult JSONValue];
for(id theKey in result){
for(id hello in theKey){
NSLog(@"The key:%@, The value:%@",hello,[theKey objectForKey:hello]);

}
}
if(TRUE){//based on some condition secondShowClassRoom is called
[self secondShowClassRoom];
}
}
}

- (void) secondShowClassRoom{

NSString *queryURL =[NSString stringWithFormat:@"http://nobert.cloudfoundry.com/CalOfEvents/to"];
NSURL *url = [NSURL URLWithString: queryURL];
NSURLRequest *req = [NSURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSError *error;
NSURLResponse *response;
NSData *returnData = [NSURLConnection sendSynchronousRequest:req returningResponse:&response error:&error];

if(returnData){
NSString *strResult = [[NSString alloc] initWithBytes:[returnData bytes] length:[returnData length] encoding:NSUTF8StringEncoding];
NSDictionary *result = [strResult JSONValue];
for(id theKey in result){
for(id hello in theKey){
NSLog(@"The key:%@, The value:%@",hello,[theKey objectForKey:hello]);
}
}
}
}

这会非常缓慢地检索数据,而我的应用程序需要非常快速的访问,因为我正在增强现实上开发应用程序

还有另一种方法更快,但我无法访问多个 URL:

- (void) showClassRoom:(NSString *)theMessage{
NSString *queryURL =[NSString stringWithFormat:@"http://nobert.cloudfoundry.com/CalOfEvents/to"];
NSURL *url = [NSURL URLWithString: queryURL];
NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];

if (conn) {
webData = [[NSMutableData data] retain];
}
classRoomControl.hidden = NO;
labControl.hidden = YES;
placementControl.hidden = YES;

}

-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *) response{

[webData setLength: 0]; }

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *) data {

[webData appendData:data];

}
-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *) error {

[conn release];
[webData release];
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection {
[conn release];
NSLog(@"DONE. Received Bytes: %d", [webData length]);

NSString *strResult = [[NSString alloc] initWithBytes:[webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
NSDictionary *result = [strResult JSONValue];

for(id theKey in result){
for(id hello in theKey){
NSLog(@"The key:%@, The value:%@",h,hello);
}
}
[strResult release];
[webData release];
}

请帮助我。任何帮助将不胜感激..

最佳答案

您正确地选择了网络通信的异步模型(因为同步通信将阻塞您的 UI,除非您在单独的线程中处理它)。

链接多个异步网络请求的正确方法是在回调中一个接一个地执行请求(即,在 connectionDidFinishLoading 中;完成一个请求后,发送下一个请求)。

还有一个建议是不要使用 NSURlRequest/NSURLConnection 来实现这一点,因为它们使思考变得不必要的复杂,并尝试使用像 AFNetworking 这样的框架。 .

关于objective-c - iOS 代码中多个 URL 检索缓慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10362004/

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