Reputation: 5834
I am Implementing a search in a JSON
data which contains data in HTML
format.
Problem is that the Data is very big.
And I have to show the matched Searches in Color as shown In Image:
For example I searched for How
:
My Searching Flow is Like that:
Step 1: - Check for presence of Data
Step 2: - Strip Html Tags from Searching Content
Step 3: - Perform Search In Content using NSRange rangeOfString options
Step 4: - If Match Found. Break Content in Sentence By using componentSeparatedByString (as I have to show the sentence in which i have found the search)
Step 5: - Pass the Sentence in Function which is returning me the
Attributed String
Step 6: - Add Results In array and reload the table
I am performing the search result in TextField
Delegate method shouldChangeCharactersInRange
The problem is I am getting my table in delay of 1 to 2 sec.
And if type to fast then Suppose I search for Animal
then I get search for ani
anim
and at last animal
.
How Can I enhance my searching.
Upvotes: 1
Views: 80
Reputation: 130102
My advice is the following:
Preparation
Steps 1 and 2. Don't repeat them for every search, do them once before starting to search or during only the first search.
Additional Step: Split the text into sentences. You will have a NSArray
of sentences as your input.
Search
Use the search text to build a NSPredicate
(e.g. contains[cd]
). Filter the input array using that predicate. That should be very fast.
Reload the table to display results. Don't bother with attributed strings here.
Highlighting
Highlight using the current search text when displaying the cell (generate an attributed string). You will highlight only what is actually displayed so this should be very fast.
Possible improvements
Debouncing - Don't start searching immediately after search text changes. Start a timer which will search in 0.1-0.5 seconds. If there is a new search text update sooner, cancel the previous timer and start a new one. This will merge several updates into one updates.
Use previous search result - if the previous search text is a substring of the new search text, you can search only the previous results, that is, less data to search.
If the search is still too slow (if you really have an enormous amount of text), perform the search in a different dispatch queue and show an activity indicator until the results are done.
Upvotes: 1