iPhone; uisearchbar 异步请求,在发送新请求之前取消

发布于 2024-10-04 23:47:36 字数 423 浏览 0 评论 0原文

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

因此,当用户开始输入四个以上字符时,系统会向在线 API 发送搜索请求,并获取国家/地区、城市信息。这里的问题是;当用户键入时,会进行呼叫,但在此之前的任何呼叫尚未结束 - 导致应用程序感觉缓慢...

重申

  1. 用户类型,让我们说“唱歌”
  2. 发送请求,并且所有城市 但即使在检索
  3. 列表之前,用户仍继续键入“Singa”。
  4. 应用程序会等待第一个请求中尚未处理的结果,甚至有时结果是一些垃圾。

我希望你们明白我的意思,我只需要取消任何待处理的请求并发送一个新的请求。我怎样才能做到这一点?

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

谢谢

Hi there guys
I am looking to have a search bar in my application,where the user searches for cities and countries - the results come from an online API.

So, when the user starts to type more than four characters, a search request is sent to the online API and country,city information is fetched. The problem here is; as the user is typing the calls are made, but any call before that has not ended yet - causing the application to feel sluggish...

To reiterate

  1. The user types, lets' say "Sing"
  2. A request is sent, and all the cities with Sing in it are retrieved
  3. But even before the list is retrieved, the user continues typing "Singa"
  4. The application kind of waits for the results that yet pending from the first request and even at times, the results are some garbage.

I hope you guys understand what i meant, i just need to cancel any pending requests and send a new one. How can i achieve this?

One way is to retrieve the list only when the user clicks "search" - but i would like it to be more interactive.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绾颜 2024-10-11 23:47:36

我在这个问题上取得了一些积极的进展......
正如 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*/

    }   
}

这对我来说效果很好 - 示例控制台输出是

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

如果您注意到当我在搜索栏中输入字符时请求被取消。

i had some positive progress with the issue...
using NSURLCOnnection, as rog mentioned, implemented a cancel before sending a new request. My code is as follows

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

    }   
}

That worked pretty good for me - a sample console output is

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

If you notice the request got cancelled as i inputted characters into the searchbar.

白云不回头 2024-10-11 23:47:36

棘手的一个!假设您正在使用 NSURLConnection,一旦检测到输入了新字符,请向其发送取消消息 [urlConnection cancel];在启动新的之前?

Tricky one! Assuming you're using NSURLConnection, once you detect a new character has been entered, send it a cancel message [urlConnection cancel]; before firing up the new one?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文