I have written a background processing logic using dispatch_async as follows:
- (IBAction)textFieldChanged:(id)sender {
dispatch_async(kBgQueue, ^{
NSArray *tempArray = [myClass getSuggestionArray]; //getSuggestionArray returns an array of objects from a database
[self performSelectorOnMainThread:@selector(initSuggestionArray:) withObject:tempArray waitUntilDone:YES];
});
}
As you can see, this method gets called everytime user edits the textfield (as soon as user types a letter). kBgQueue is defined as:
#define kBgQueue dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
What I want is some method, some logic so that whenever user edits the field I can check whether there is already a queued task in this kBgQueue or not and if it is, then I want to stop the execution of that queued job before starting the next block.(want to remove the previous one) I am using this so that the UI of the application doesn't look like it hanged, if user types 3-4 characters quickly (because result is coming too late from my database)