Reputation: 91
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.
Upvotes: 1
Views: 1198
Reputation: 20410
A good approach can be to reload the table only with the data filtered by the search.
NSArray
with your valuesNSArray
for your results filteredThen 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];
}
}
Now you reload the table with arrayOfResults
instead of arrayOfAll
Upvotes: 3
Reputation: 692
Don't do a search on each letter but rather when the user taps on a "Search" button.
Upvotes: 0
Reputation: 5393
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).
Upvotes: 0
Reputation: 461
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.
Upvotes: 2