Search huge data comes from Web Services in iPhone

1.1k Views Asked by At

I have to develop an app which should handle huge amount of data which is come from web service call.

There is a UITableView with search bar, When ever the user type a letter, the tableview should dynamically display the search result corresponding to the text in the search bar.

What is the best way to handle this kind of data transfer? Sending request every time when the user type a letter seems to be a bad idea.

4

There are 4 best solutions below

0
On

Don't do a search on each letter but rather when the user taps on a "Search" button.

0
On

A good approach can be to reload the table only with the data filtered by the search.

  1. You have a NSArray with your values
  2. Then you create another NSArray for your results filtered
  3. Then you go through the first array like this:

    for (int i = 0; i < [arrayOfAll count]; i++){
    
        NSString *sTemp = [arrayOfAll objectAtIndex:i];
    
        NSRange titleResultsRange = [sTemp rangeOfString:searchText
                                                 options:NSCaseInsensitiveSearch];
    
        if (titleResultsRange.length > 0){
            [arrayOfResults addObject:sTemp];
        }
    }
    
  4. Now you reload the table with arrayOfResults instead of arrayOfAll

0
On

Yes, to send a request every time the user types a letter is a bad idea... but, if for example you implements it using NSOperationQueues you can cancel previous requests when a key is tapped (any character or backspace) then you will only have one request going to the server. It's just a suggestion and please take a lot of consideration in implementing it anyway.

Obviously the simplest way is as Prashant says.

@tonio As I understand, you are assuming you have all the data in an NSArray already and you just filter the results parsing with an NSRange. As Karthik said, it's a huge amount of data from the web service and it might take a lot of time to load and is unnecessary network traffic. But lets say that this is the way you want to do it, I would suggest to filter the results using an NSPredicate:

NSPredicate *predicate = [NSPredicate predicateWithFormat: @"SELF contains[cd] %@",searchText];
NSArray *filteredResults = [arrayOfAll filteredArrayUsingPredicate: predicate];

Now you use the filteredResults array in the TableView.

0
On

It kind of depends on how big is "huge" and how the users will use the app.

If it takes a few minutes to the load the entire data set then I would be inclined to get the smaller amount of data piece meal e.g. every time the search string changes.

If the data is not that "huge" then go with tonio.mg' suggestion if you don't want to have a "search" button like Prashant Bhayani suggested (his is probably the best solution for mobile devices imho).

Remember the important thing is not how you "want" to implement this functionality but how your end-users want this functionality to work for them. Typically users don't want to wait (for a huge data set) they do want quick easy and up-to date data access (lazy loading).