我正在编写一个iOS应用程序,它从Web服务加载数据并将数据放入表视图中。用户可以通过单击单元格然后选择值来更新单元格中的数据。选择该值后,我希望表更新以反映新数据,实质上是从Web服务重新初始下载数据。在更新方法结束时,我调用我的初始加载方法,但这似乎不起作用。它只是重新加载所有原始数据。这是我的更新方法的结束:
self.connectionInProgress = [[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease];
[self loadLines];
负载线方法:
- (void)loadLines
{
NSLog(@"going again?");
[Results removeAllObjects];
[Results release];
Results = nil;
Results = [[NSMutableArray alloc] init];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSString *jsonString = [self performFetchWithURL:lineURL];
if(jsonString == nil) {
[self showNetworkError];
return;
}
NSDictionary *dictionary = [self parseJSON:jsonString];
if(dictionary == nil) {
dispatch_async(dispatch_get_main_queue(), ^{
[self showNetworkError];
});
return;
}
[self parseDictionary:dictionary];
dispatch_async(dispatch_get_main_queue(), ^{
isLoading = NO;
[self.tableView reloadData];
});
});
}
parseDictionary和performFetchWithURL是iOS 5中相当标准的JSON交互...将数据放入Results数组中。我是通过清除Results数组然后重新创建它来做正确的事吗?
我如何获得全新数据?
答案 0 :(得分:0)
我强烈建议调查ASIHTTPRequest。它可能会或可能不是您的问题的解决方案,但它将删除大量不必要的代码,使您的项目更容易调试。 GCD非常好,但当你发送垃圾邮件时,它会很快失控并导致你遇到的奇怪行为。
http://allseeing-i.com/ASIHTTPRequest/How-to-use
//Unsafe_unretained is for ARC which it doesn't look like you are using.
__unsafe_unretained ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:lineURL]];
// Code inside of this block will run when the request finishes
// This code will also be called to on the main thread. So you can avoid your own GCD
[request setCompletionBlock:^{
if (![request responseString])
{
[self showNetworkError];
return;
}
NSDictionary *dictionary = [self parseJSON:[request responseString]];
if(dictionary == nil) {
[self showNetworkError];
return;
}
[self.tableView reloadData];
}];
//This code will run if your request fails
[request setFailedBlock:^{
NSLog(@"Failed %@", [request error]);
}];
[request startAsynchronous];//Start the request
我会试一试。如果这不能解决您的问题,那么很可能您的问题出现在您尚未发布的代码中。
我为该代码段中的任何语法错误道歉,我没有编译器方便。