2010-11-28 21 views
2

嗨有朋友 我正在尋找在我的應用程序中有一個搜索欄,其中用戶搜索城市和國家 - 結果來自在線API。iphone;在發送一個新的請求之前取消發件人的郵箱

因此,當用戶開始鍵入四個以上的字符時,搜索請求會被髮送到在線API和國家,城市信息被提取。這裏的問題是;如用戶鍵入的通話製造,但在此之前的任何呼叫還未結束 - 導致應用程序遲緩的感覺......

重申

  1. 的用戶類型,讓說「唱「
  2. 的請求被髮送,並且所有Sing在它的城市中檢索
  3. 但檢索到的名單,甚至之前,用戶繼續輸入‘辛加’
  4. 應用那種等待的對結果尚未等待第一次請求,甚至有時,結果如此我垃圾。

我希望你們明白我的意思,我只需要取消任何未決的請求併發送一個新的請求。我怎樣才能做到這一點?

一種方法是僅當用戶點擊「搜索」時才檢索列表 - 但我希望它更具互動性。

謝謝

回答

0

棘手的一個!假設你使用NSURLConnection,一旦你檢測到一個新的字符被輸入,發送一個取消消息[urlConnection取消];在啓動新的之前?

+0

我會想到這一點 - 將嘗試實施。我認爲這將是一個普遍的問題,並會有一個鍋爐板解決方案:) – Veeru 2010-11-28 06:47:19

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&[email protected]$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 

如果您發現該請求被取消了,因爲我輸入的字符到搜索欄。

相關問題