苹果手机; uisearchbar异步请求,在发送新请求之前取消

时间:2010-11-28 05:39:17

标签: iphone uisearchbar

大家好 我希望在我的应用程序中有一个搜索栏,用户可以搜索城市和国家/地区 - 搜索结果来自在线API。

因此,当用户开始输入超过四个字符时,会向在线API和国家/地区发送搜索请求,并提取城市信息。这里的问题是;当用户正在键入调用时,但之前的任何调用尚未结束 - 导致应用程序感到迟钝......

重申

  1. 用户输入,让我们说“唱”
  2. 发送请求,并检索其中包含Sing的所有城市
  3. 但即使在检索列表之前,用户仍继续键入“Singa”
  4. 应用程序类型等待第一个请求尚未处理的结果,甚至有时候结果都是垃圾。
  5. 我希望你们明白我的意思,我只需要取消任何待处理的请求并发送一个新请求。我怎样才能做到这一点?

    一种方法是仅在用户点击“搜索”时检索列表 - 但我希望它更具互动性。

    由于

2 个答案:

答案 0 :(得分:3)

我在这个问题上取得了一些积极进展...... 使用NSURLCOnnection,如提到的rog,在发送新请求之前实现了取消。我的代码如下

- (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*/

    }   
}

这对我来说非常好 - 一个示例控制台输出

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

如果您注意到请求被取消,因为我将字符输入搜索栏。

答案 1 :(得分:0)

整蛊一个!假设你正在使用NSURLConnection,一旦你检测到一个新的字符被输入,就发送一条取消消息[urlConnection cancel];在启动新的之前?