Reputation: 77
// MARK: - Search
func updateSearchResultsForSearchController(searchController: UISearchController) {
if self.searchController.searchBar.text!.characters.count < 3 {
print("<3")
}else{
Kinopoisk.searchFilmByString(self.searchController.searchBar.text!) { (JSON) in
for index in 0...5 {
let nameRU = JSON["searchFilms"][index]["nameRU"].string ?? ""
let nameEN = JSON["searchFilms"][index]["nameEN"].string ?? ""
let plusString = "\(nameRU)/\(nameEN)"
self.movies.append(plusString)
print(self.movies[index])
}
}
}
self.filteredMovies = self.movies.filter { (movie: String) -> Bool in
if movie.lowercaseString.containsString(self.searchController.searchBar.text!.lowercaseString) {
return true
} else {
return false
}
}
self.resultsController.tableView.reloadData()
}
I have two arrays(movies and filteredMovies). The first recorded data from JSON (API), the second contained sorted data from first array.
Problem 1: When I start to enter searchBar's field starts downloading from API to first array, but if i add new character to field old data will stay. How delete data in first array after search request?
Problem 2: API have limit for search by >2 character - How to start search only when there is more than two symbols in SearchBar.text?
Problem 3: Search start only when i deselect from enter. - How to search while you type rather than after?
Upvotes: 2
Views: 1496
Reputation: 148
Well if you want to do things right, you should use a reactive framework. Most people are hot on ReactiveCocoa right now, but a lot of people also use RxSwift. I am more of a RAC guy so I will point you in that direction.
Colin Eberhardt wrote a blog post about making an app that does basically this exact thing with twitter searching. He also has another similar RAC flickr app. He uses RAC3 which is only slightly different (mostly syntax changes) from RAC4. I would highly suggest learning about and beginning to implement Functional Reactive techniques in your apps.
Cheers!
Upvotes: 2