Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7618955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:32:17+00:00 2026-05-31T03:32:17+00:00

i’m searching implementing a UISearchbar that retrieve information from a url, and with the

  • 0

i’m searching implementing a UISearchbar that retrieve information from a url, and with the default method:

 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{

i can detect immediately when the text change and perform the fetch of url, but in this way, the text type is slow because the iPhone is searching the url, so i want start the fetching of the url when the user is stop writing for some second, so i want detect the pause of the typing to refresh the table view retrieving the information by the url. i have found a older post of this request and i have tried a solution that for me don’t work:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(request:)     object:searchText];

    //.....

    [self performSelector:@selector(request:) withObject:searchText afterDelay:1.5];
}

-(void)request:(NSString *)myString
{
    NSLog(@"%@",myString);
}

in this way when i’m typing the request method is not called but when i stop typing it’s called for every character i type, so is the same of the default method, i wrong something? or the implementation isn’t correct?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T03:32:19+00:00Added an answer on May 31, 2026 at 3:32 am

    It looks like the provided solution isn’t working because the “searchText” argument to cancelPreviousPerformRequestsWithTarget:selector:object: doesn’t match the “searchText” argument to the previous call to performSelector:withObject:afterDelay:; so delayed searches get queued up every text change, and never cancelled.

    You could use an NSTimer to delay the invocation of your search, and cancel the timer whenever the text changes: Give your object an NSTimer property or field; in searchBar:textDidChange: cancel any existing timer, then create and schedule a new one. The target of the timer should call your search method.

    Something like (off the top of my head):

    // in your class' .h object fields
    {
      ...
      NSTimer *searchDelayer; // this will be a weak ref, but adding "[searchDelayer invalidate], searchDelayer=nil;" to dealloc wouldn't hurt
      ...
    }
    
    // in the .m
    
    -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
      [searchDelayer invalidate], searchDelayer=nil;
      if (YES /* ...or whatever validity test you want to apply */)
        searchDelayer = [NSTimer scheduledTimerWithTimeInterval:1.5
                                                         target:self
                                                       selector:@selector(doDelayedSearch:)
                                                       userInfo:searchText
                                                        repeats:NO];
    }
    
    -(void)doDelayedSearch:(NSTimer *)t
    {
      assert(t == searchDelayer);
      [self request:searchDelayer.userInfo];
      searchDelayer = nil; // important because the timer is about to release and dealloc itself
    }
    

    Some programmers might be less cavalier about weak references than I’m being here.

    [edited to add:]

    Here’s how you might do it if you were set on using cancelPreviousPerformRequestsWithTarget…:

    // in your class' .h object fields
    {
      ...
      NSString *priorSearchText; // this will NOT be a weak ref, so adding "[priorSearchText release]" to dealloc is also required
      ...
    }
    
    // in the .m
    -(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
    {
      [NSObject cancelPreviousPerformRequestsWithTarget:self
                                               selector:@selector(request:)
                                                 object:priorSearchText];
      [priorSearchText release], priorSearchText = [searchText retain];
      if (YES /* ...or whatever validity test you want to apply */)
        [self performSelector:@selector(request:)
                   withObject:searchText
                   afterDelay:1.5];
    }
    

    I don’t think I’ve ever used cancelPreviousPerformRequestsWithTarget:… for real, so I don’t know if it hides any surprises. If you have trouble, add NSLogs, look for delayed searches not getting cancelled when they should.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am currently running into a problem where an element is coming back from
I am doing a simple coin flipping experiment for class that involves flipping a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.