Question

I have a well-functioning search bar. Only one thing that I want to improve is NSTimer. I don`t know why, but when I start typing, it search me results after some delay.It woks perfectly. But I get for every typed letter 1 API each. It mean that if I put into search bar for example Sudan. I get 5 API instead of 1. Could you someone help me? Thanks

TableViewControllerTOP.h

@interface TableViewControllerTOP : UITableViewController<UISearchDisplayDelegate, UISearchBarDelegate,UIScrollViewDelegate,UITableViewDataSource, UITableViewDelegate>
{

    NSTimer *myTimer;
}
@property (strong, nonatomic) IBOutlet UITableView *MainTable;
@property (nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic, strong) UISearchDisplayController *searchController;
@property (nonatomic, strong) NSMutableArray *searchResults;

@property (nonatomic, retain) NSTimer *myTimer;
@property (nonatomic, assign) BOOL isAscending;

TableViewControllerTOP.m

@interface TableViewControllerTOP ()
@property (strong,nonatomic) NSMutableArray *itemss;

@end

@implementation TableViewControllerTOP
@synthesize myTimer;
@synthesize searchBar;
@synthesize searchController;
@synthesize searchResults;

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {

    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.isAscending = YES;
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(pulltorefresh) forControlEvents:UIControlEventValueChanged];
    self.refreshControl = refreshControl;
      PFQuery *query = [PFQuery queryWithClassName:@"Countries"];
    query.cachePolicy = kPFCachePolicyIgnoreCache;
        [query addDescendingOrder:@"createdAt"];



    [query setLimit:10];

    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {



        if (!error) {
            NSMutableArray *items = [NSMutableArray array];
            for (id obj in objects)
            {
                [items addObject:obj];
            }
            self.itemss = items;

            NSLog(@"%@", objects);
            NSLog(@"%lu", (unsigned long)[objects count]);
            [_MainTable reloadData];
        } else {

            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];



[self searchitems];

}

-(void)searchitems

{
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];

    self.tableView.tableHeaderView = self.searchBar;

    self.searchController = [[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self];

    self.searchController.searchResultsDataSource = self;
    self.searchController.searchResultsDelegate = self;
    self.searchController.delegate = self;



    self.searchResults = [NSMutableArray array];

    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background.png"]];
    [self.navigationController.navigationBar setBarTintColor:[UIColor lightGrayColor]];




}



-(void)filterResults:(NSString *)searchTerm {



    myTimer = [NSTimer scheduledTimerWithTimeInterval:3.0
                                               target:self
                                             selector:@selector(queryask)
                                             userInfo:_itemss
                                              repeats:NO];



}


-(void)queryask{
    PFQuery *queryCapitalizedString = [PFQuery queryWithClassName:@"Countries"];
    [queryCapitalizedString whereKey:@"CountryTitle" containsString:[searchBar.text capitalizedString]];

    PFQuery *queryLowerCaseString = [PFQuery queryWithClassName:@"Countries"];
    [queryLowerCaseString whereKey:@"CountryTitle" containsString:[searchBar.text lowercaseString]];

    PFQuery *querySearchBarString = [PFQuery queryWithClassName:@"Countries"];
    [querySearchBarString whereKey:@"CountryTitle" containsString:searchBar.text];

    PFQuery *finalQuery = [PFQuery orQueryWithSubqueries:[NSArray arrayWithObjects:queryCapitalizedString,queryLowerCaseString, querySearchBarString,nil]];

    [finalQuery findObjectsInBackgroundWithTarget:self selector:@selector(callbackWithResult:error:)];



}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}



- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    [self filterResults:searchString];
    return YES;
}

- (void)callbackWithResult:(NSArray *)items error:(NSError *)error
{
    if(!error) {
        [self.searchResults removeAllObjects];
        [self.searchResults addObjectsFromArray:items];
        [self.searchDisplayController.searchResultsTableView reloadData];



    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (tableView == self.tableView) {


        return self.itemss.count;

    } else {

        return self.searchResults.count;

    }
}






- (void)searchDisplayController:(UISearchDisplayController *)controller didLoadSearchResultsTableView:(UITableView *)tableView
{
    tableView.rowHeight = 70.0f;
}


- ( UITableViewCell * ) tableView : ( UITableView * ) tableView cellForRowAtIndexPath : ( NSIndexPath * ) indexPath
{

    static NSString *uniqueIdentifier = @"MainCell";

CustomCell3 *cell = nil;
    cell = (CustomCell3 *) [self.tableView dequeueReusableCellWithIdentifier:uniqueIdentifier];




    if (!cell) {
        NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MainCell" owner:nil options:nil];

        for (id currentObject in topLevelObjects)
        {
            if([currentObject isKindOfClass:[CustomCell3 class]])
            {
                cell = (CustomCell3 *)currentObject;
                break;
            }
        }
    }

    if (tableView == self.tableView) {
        cell.MainTitle.text = [[self.itemss objectAtIndex:indexPath.row] objectForKey:@"CountryTitle"];
        cell.DescriptTitle.text = [[self.itemss objectAtIndex:indexPath.row] objectForKey:@"DescriptTitle"];
        [cell.FlagTitle setImageWithURL:[NSURL URLWithString:[[self.itemss objectAtIndex:indexPath.row] objectForKey:@"ImaURL"]]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];




    }






    if(tableView == self.searchDisplayController.searchResultsTableView) {

        PFObject *searchedUser = [self.searchResults objectAtIndex:indexPath.row];
        NSString *content = [searchedUser objectForKey:@"CountryTitle"];
        NSString *desco = [searchedUser objectForKey:@"DescriptTitle"];
        cell.DescriptTitle.text = desco;
        cell.MainTitle.text = content;
        NSString *image = [searchedUser objectForKey:@"ImaURL"];

        [cell.FlagTitle setImageWithURL:[NSURL URLWithString:image]
                       placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
        cell.backgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];
        cell.selectedBackgroundView =  [[UIImageView alloc] initWithImage:[ [UIImage imageNamed:@"background.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:5.0] ];


    }



    return cell;
}



@end
Was it helpful?

Solution

For a way to delay the search, take a look at this SO question: Delay UISearchbar parsing

A good idea could be to combine this with Logan's suggestion. A search should often not be performed until a minimum of characters have been entered, to avoid too broad searches (not much point performing a search for anything containing "s" in many cases, depending on what you are searching).

OTHER TIPS

Perhaps instead of a timer, you could implement a search bar delegate to run a query after some custom logic, perhaps every 3 letters?

- (void) searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
    if ((searchBar.text.length % 3) == 0) {
        [self queryAsk];
    } 
}

Note: This logic isn't the best, and you might want to explore various parameters for when to search, but I think the concept improves over the timer because it would be more based on input. I think it would give you more control.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top