Iphone 苹果手机;uisearchbar异步请求,在发送新请求之前取消
嗨,伙计们 我希望在我的应用程序中有一个搜索栏,用户在其中搜索城市和国家-结果来自在线API 因此,当用户开始键入四个以上的字符时,将向在线API发送搜索请求,并获取城市信息。问题是,;当用户键入时,会进行调用,但在此之前的任何调用都尚未结束-导致应用程序感觉迟钝 重申Iphone 苹果手机;uisearchbar异步请求,在发送新请求之前取消,iphone,uisearchbar,Iphone,Uisearchbar,嗨,伙计们 我希望在我的应用程序中有一个搜索栏,用户在其中搜索城市和国家-结果来自在线API 因此,当用户开始键入四个以上的字符时,将向在线API发送搜索请求,并获取城市信息。问题是,;当用户键入时,会进行调用,但在此之前的任何调用都尚未结束-导致应用程序感觉迟钝 重申 用户输入,让我们说“Sing” 发送一个请求,并检索其中包含Sing的所有城市 但即使在检索列表之前,用户仍继续键入“Singa” 应用程序会等待第一个请求尚未处理的结果,甚至有时,结果是一些垃圾 我希望你们能理解我的意思,我只
谢谢棘手的问题!假设您正在使用NSURLConnection,一旦检测到输入了新字符,请向其发送一条取消消息[urlConnection cancel];在启动新版本之前?我在这个问题上取得了一些积极的进展。。。 正如rog所提到的,使用NSURLCOnnection在发送新请求之前实现了一个取消。我的代码如下
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
if([searchText length]<4) return;
else{
//[tableData removeAllObjects];// remove all data that belongs to previous search
//make the call
url=[NSString stringWithFormat:@"http://xml.customweather.com/xml?client=clinique_test&client_password=f@c3$toF&product=search&search=%@",searchBar.text];
NSURLRequest *request=[NSURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
NSLog(@"%@",url);
if(connection!=nil){ //cancel if in process
NSLog(@"connection cancelled");
[connection cancel];
}
connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Making request");
}
}
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)incrementalData {
if (data==nil) {
data =
[[NSMutableData alloc] initWithCapacity:2048];
}
[data appendData:incrementalData];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection {
NSString *res=[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",res);
[connection release];
connection=nil;
[data release];
data=nil;
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
/* MY custom function goes here*/
}
}
-(无效)搜索栏:(UISearchBar*)搜索栏文本更改:(NSString*)搜索文本
{
如果([searchText length]我也会想到这一点-我会尝试实施。我认为这将是一个常见问题,并且会有一个锅炉板解决方案:)让我们知道你是怎么做的,有趣的问题,所以我会投票给你,我会尝试你提到的解决方案,看看它是如何进行的,很快就会公布结果
2010-11-29 15:46:52.535 searchBar1[2931:207] Making request
2010-11-29 15:46:52.678 searchBar1[2931:207] connection cancelled
2010-11-29 15:46:52.690 searchBar1[2931:207] Making request
2010-11-29 15:46:52.871 searchBar1[2931:207] connection cancelled
2010-11-29 15:46:52.876 searchBar1[2931:207] Making request
2010-11-29 15:46:53.063 searchBar1[2931:207] connection cancelled
2010-11-29 15:46:53.069 searchBar1[2931:207] Making request
2010-11-29 15:46:53.367 searchBar1[2931:207] connection cancelled
2010-11-29 15:46:53.372 searchBar1[2931:207] Making request
2010-11-29 15:46:53.529 searchBar1[2931:207] connection cancelled
2010-11-29 15:46:53.535 searchBar1[2931:207] Making request
2010-11-29 15:46:54.354 searchBar1[2931:207] <?xml version="1.0" encoding="UTF-8"?>
<cw_citylist size="1" search_type="city">
<city id="75281" name="Singapore" state="" state_name="" country="SN" country_name="Singapore" lat="1.28" long="103.84" population="2930200" timezone="8" timezone_code="SGT" timezone_id="Asia/Singapore" localtime="Mon, 29 Nov 2010 15:50:43 SGT" region="Indonesia" weather_id="75281" />
</cw_citylist>
2010-11-29 15:46:54.360 searchBar1[2931:207] Searching for ... city
2010-11-29 15:46:54.364 searchBar1[2931:207] Found in Singapore, Singapore
2010-11-29 15:46:54.368 searchBar1[2931:207] contacts error in num of row
2010-11-29 15:46:54.374 searchBar1[2931:207] Array value is Singapore,Singapore