在iphone中按下每个键时调用webservice

时间:2011-08-15 11:13:32

标签: iphone web-services asynchronous uisearchbar

我正在使用搜索栏,每当我在搜索栏中输入文本时,它应该采用该字符串,它将形成一个请求字符串并生成输出。要更清楚地假设,如果我按'c'我想要显示用c盯着的城市名称,然后在搜索栏中仍然出现'c'我按'h',所以现在字符串是'ch'所以使用web服务我正在搜索以'ch'开头的citiy名称'。我使用JSON类型的web服务,并在表视图中填充响应

我的问题是,当我慢慢按下按键时,搜索工作正常完成,但是当快速按下按键时,即按下'c'然后快速按下'h'。网络服务被访问'ch' '然后'c'..这样就可以打印以c开头的城市而不是CH ..

我按以下方式在每个按键上调用Web服务:

语句调用

中的web服务
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchString {

....

...
 [NSThread detachNewThreadSelector: @selector(callService) toTarget:self withObject:nil];
}

and inside the callService method.....

- (void)callService{
    NSAutoreleasePool *pool = [ [ NSAutoreleasePool alloc ] init ];
    [self performSelectorOnMainThread:@selector(initializeRequest) withObject:nil waitUntilDone:NO]; 
    [pool release];

}

- (void)initializeRequest {


....Here i form the request string...
create connection and after creating connection i use NSURLConnectionDelegate Methods to parse the connection
}

有没有办法顺利进行搜索。

当搜索栏中的文字发生变化时,请任何机构向我建议任何其他替代或方法来动态执行此类搜索。谢谢你们所有人

1 个答案:

答案 0 :(得分:3)

跟踪您的连接,当您形成新请求时,请先取消旧连接。

[prevURLConnection cancel];