我正在我的App Delegate类中异步下载JSON订阅源。现在数据需要一段时间才能加载,因此我的表格视图首先显示为空,然后在几秒钟后填充。因此,我想要:异步加载JSON并显示活动指标视图
1-找出导致此延迟的原因。因此,请保留应用程序中的所有活动:didFinishLaunchingWithOptions方法,并且只加载所有内容后才加载VC。
OR
2-显示活动性指示符直到表填充数据。
现在在第一种情况下,我非常肯定我在错误的时间推视图控制器。我尝试过使用它,但似乎这是我的应用程序构建和运行的唯一方式。
在第二种情况下,我想知道哪个“连接”方法首先被调用,哪一个最后被调用。因此,我将能够在第一种方法中启动活动指示器视图,并在最后一种方法结束时释放。
以下是我的代码。任何建议/帮助非常感谢。谢谢你的阅读。
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error"
message:@"Please check your network connection and relaunch the application"
delegate:self
cancelButtonTitle:@"Dismiss"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
if ([responseString isEqualToString:@"Unable to find specified resource."]) {
NSLog(@"Unable to find specified resource.n");
}
else {
ListingsViewController *listingsViewController = [[ListingsViewController alloc] initWithNibName:@"ListingsViewController" bundle:nil];
listingsViewController.jsonData = responseString;
[self.navigationController pushViewController:listingsViewController animated:NO];
[self.navigationController setViewControllers:[NSArray arrayWithObject:listingsViewController] animated:NO];
[listingsViewController release];
}
[connection release];
[responseData release];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Start the HTTP request
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:
[NSURL URLWithString:@"http://www.shoofeetv.com/iphonexml/view/all_channels.json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
// Display the navigation controller
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
将所有aysnc http请求包装到另一个方法或另一个类中?你知道我在哪里可以找到一个关于objective-c http异步方法的好教程吗?我到处寻找,我似乎还没有很好地理解它。谢谢.. – darksky 2011-06-13 08:24:38
在另一个类 - 例如称之为HttpClient。老实说,最好的地方是苹果文档http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html - 阅读所有的方法你可以实现和每个人都做什么。如果你真的无法理解它(或者根本不能被打扰),请查看http://allseeing-i.com/ASIHTTPRequest/How-to-use - 特别是异步的东西。这是NSURLConnection的一个很好的包装类,应该让你的生活更轻松。 – Tyler 2011-06-13 08:55:43
谢谢泰勒.. – darksky 2011-06-13 11:57:58