Issue with duplicate search results and XMLParser array refresh
I use an NSXMLParser to parse ext. API data. I also use a Singleton shared array var to maintain info retrieved from ext API.
This info is dynamic and changesas the user types anything in UISearchBar
Now the thing is as soon as the user types anything, I clear off the existing array contents by using [retrievedArray removeAllObjects];
Thus this refreshed retrievedArray based on the current terms in the search bar.
This works fine if the user types slowly. However I get to see duplicate search results if the user types very fast. This, I am assuming, is because the retrievedArray contents do not get enough time to clear off.
I am still trying to resolve the issue. Plea开发者_C百科se suggest some more fixes.
Could you please provide me the fix.
No, I don't think this is the case, unless you're doing search in a separate thread and clear array in another one. If not, then there is probably error in your search logic.
First of all I think a singleton approach may not be the best way to go for what you are doing. But make sure that you are synchronizing all mutable access to the array. Instead of allowing the singleton to return a NSMutableArray
for any object and their mom (super
) to use you need to have methods like addObject
,removeObject
,clear
with @synchronize
blocks or any kind of lock you decide on. I still see issues with this approach because the code calling addObject, remove and clear will all need to be synchronized as well. Maybe consider on each auto complete request you use a delegate or post a NSNotification
containing a timestamp, the characters the user typed to get data, and a NSArray
of results. At that time you can see if the response is still valid, discard any invalid responses, and update the user with just the contents of the most recent valid NSArray
精彩评论