Skip to content

Instantly share code, notes, and snippets.

@asvechkar
Created August 1, 2016 08:34
Show Gist options
  • Save asvechkar/851a7c2f4a06d0a7b260fc659a070a04 to your computer and use it in GitHub Desktop.
Save asvechkar/851a7c2f4a06d0a7b260fc659a070a04 to your computer and use it in GitHub Desktop.
Отзывчивый searchbar
func searchBar(searchBar: UISearchBar, textDidChange searchText: String) {
weak var weakSelf: ProductPickerTableViewController = self
var searchDelay: Double = 0.3
if self.searchBlock != nil {
//We cancel the currently scheduled block
cancel_block(self.searchBlock)
}
self.searchBlock = dispatch_after_delay(searchDelay, {() -> Void in
//We "enqueue" this block with a certain delay. It will be canceled if the user types faster than the delay, otherwise it will be executed after the specified delay
weakSelf.currentSearchOperation.cancel()
weakSelf.currentSearchOperation = NSBlockOperation.blockOperationWithBlock({() -> Void in
if searchText.length() {
weakSelf.searchProducts = weakSelf.food.productsBySearchPhrase(searchText)
weakSelf.isSearchNowProducts = true
}
else {
weakSelf.isSearchNowProducts = false
}
dispatch_async(dispatch_get_main_queue(), {() -> Void in
weakSelf.tableView.reloadData()
weakSelf.currentSearchOperation = nil
})
})
let label: Character = "ru.rinatabidullin.unique.search"
var queue: dispatch_queue_t = dispatch_queue_create(label, DISPATCH_QUEUE_SERIAL)
dispatch_async(queue, {() -> Void in
weakSelf.currentSearchOperation.start()
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment