如何在我的searchcontroller中使用queryStartingAtValue搜索用户? [英] How can I use queryStartingAtValue in my searchcontroller to search for users?
问题描述
我之前曾问过一个有关以最经济高效的方式搜索用户(而不必加载整个数据库中的每个用户)的问题.
I have previously asked a question about searching for users the most cost-efficient way (without having to load up every user in the entire database.
我的问题之前的代码是
class FollowUsersTableViewController: UIViewController{
@IBOutlet var tableView: UITableView!
private var viewIsHiddenObserver: NSKeyValueObservation?
let searchController = UISearchController(searchResultsController: nil)
var usersArray = [UserModel]()
var filteredUsers = [UserModel]()
var loggedInUser: User?
//
var databaseRef = Database.database().reference()
//usikker på den koden over
override func viewDidLoad() {
super.viewDidLoad()
//large title
self.title = "Discover"
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
} else {
// Fallback on earlier versions
}
self.tableView?.delegate = self
self.tableView?.dataSource = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.searchController.delegate = self;
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
self.loadProfileData()
}
func loadProfileData() {
databaseRef.child("profile").queryOrdered(byChild: "username").observe(.childAdded, with: { (snapshot) in
print(snapshot)
let userObj = Mapper<UserModel>().map(JSONObject: snapshot.value!)
userObj?.uid = snapshot.key
guard snapshot.key != self.loggedInUser?.uid else { return }
self.usersArray.append(userObj!)
})
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let dest = segue.destination as! UserProfileViewController
let obj = sender as! UserModel
let dict = ["uid": obj.uid!, "username": obj.username!, "photoURL": obj.photoURL, "bio": obj.bio]
dest.selectedUser = dict as [String : Any]
}
}
// MARK: - tableview methods
extension FollowUsersTableViewController: UITableViewDataSource,
UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection
section: Int) -> Int {
return searchController.searchBar.text!.count >= 2 ?
filteredUsers.count : 0
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! FollowTableViewCell
let user = filteredUsers[indexPath.row]
cell.title?.text = user.username
if let url = URL(string: user.photoURL ?? "") {
cell.userImage?.sd_setImage(with: url, placeholderImage:
#imageLiteral(resourceName: "user_male"), options:
.progressiveDownload, completed: nil)
cell.userImage.sd_setIndicatorStyle(.gray)
cell.userImage.sd_showActivityIndicatorView()
}
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath:
IndexPath) -> CGFloat {
return 50
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath:
IndexPath) {
self.performSegue(withIdentifier: "user", sender: self.filteredUsers[indexPath.row])
}
}
// MARK: - search methods
extension FollowUsersTableViewController:UISearchResultsUpdating,
UISearchControllerDelegate {
func updateSearchResults(for searchController: UISearchController) {
searchController.searchResultsController?.view.isHidden = false
filterContent(searchText: self.searchController.searchBar.text!)
self.tableView.reloadData()
}
func filterContent(searchText:String){
if searchText.count >= 2{
self.filteredUsers = self.usersArray.filter{ user in
return(user.username!.lowercased().contains(searchText.lowercased()))
}
}
}
}
但是随后用户"maxwell"回复了我,并建议使用queryStartingAtValue像这样:
But then user "maxwell" replied to me and suggested to use queryStartingAtValue like this:
func searchQueryUsers(text: String, completion: @escaping (_ userNames: [String]) -> Void) {
var userNames: [String] = []
databaseRef.child("profile").queryOrdered(byChild: "username").queryStarting(atValue: text).observeSingleEvent(of: .value, with: { snapshot in
for item in snapshot.children {
guard let item = item as? DataSnapshot else {
break
}
//"name" is a key for name in FirebaseDatabese model
if let dict = item.value as? [String: Any], let name = dict["name"] as? String {
userNames.append(name)
}
}
completion(userNames)
})
}
如何使用Mapper<UserModel>().map(JSONObject)
已有的代码来实现此目的?我尝试实现他的代码,却找不到真正有效的方法,而且似乎无法进入maxwell,所以有人可以帮助我吗?
How can I implement this, with my already existing code with my Mapper<UserModel>().map(JSONObject)
? I tried implementing his code and couldn't really find a way to do this effectively, and I can't seem to get to maxwell, so can anyone help me out with this?
我应该在maxwells代码中使用searchController.searchBar.text作为文本"吗?
Should i use searchController.searchBar.text for the "text" in maxwells code?
谢谢
为杰伊(Jay)更新:
Updated for Jay:
class FollowUsersTableViewController: UIViewController,
UISearchBarDelegate {
@IBOutlet var tableView: UITableView!
private var viewIsHiddenObserver: NSKeyValueObservation?
let searchController = UISearchController(searchResultsController: nil)
var usersArray = [UserModel]()
var filteredUsers = [UserModel]()
var loggedInUser: User?
//
var databaseRef = Database.database().reference()
//usikker på den koden over
override func viewDidLoad() {
super.viewDidLoad()
//large title
self.title = "Discover"
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
} else {
// Fallback on earlier versions
}
self.tableView?.delegate = self
self.tableView?.dataSource = self
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
self.searchController.delegate = self;
definesPresentationContext = true
tableView.tableHeaderView = searchController.searchBar
//self.loadProfileData()
//self.searchBar(searchController.searchBar, textDidChange: searchController.searchBar.text)
}
func searchUsers(text: String) {
if text.count > 0 {
self.usersArray = [] //clear the array each time
let endingText = text + "\u{f8ff}"
databaseRef.child("profile").queryOrdered(byChild: "username")
.queryStarting(atValue: text)
.queryEnding(atValue: endingText)
.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let childSnap = child as! DataSnapshot
let userObj = Mapper<UserModel>().map(JSONObject: childSnap.value!)
userObj?.uid = childSnap.key
if childSnap.key != self.loggedInUser?.uid { //ignore this user
self.usersArray.append(userObj!)
}
}
self.tableView.reloadData()
})
}
} //may need an else statement here to clear the array when there is no text
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
let dest = segue.destination as! UserProfileViewController
let obj = sender as! UserModel
let dict = ["uid": obj.uid!, "username": obj.username!, "photoURL": obj.photoURL, "bio": obj.bio]
dest.selectedUser = dict as [String : Any]
}
func searchBar(_ searchBar: UISearchBar,
textDidChange searchText: String) {
self.searchUsers(text: searchText)
}
}
推荐答案
问题中的代码几乎正确,只需对其进行调整以确保枚举var是快照,然后将其与Mapper一起使用.
The code in the question is almost correct, just need to tweak it to ensure the enumeration var is a snapshot and then use it with Mapper.
当用户输入用户名时,每次按键都会递归调用它;因此,如果他们在Leroy中键入,则首先键入L,此代码将检索用户名值以"L"开头的所有节点.然后,用户输入"e",使其变为"Le"等.
As a user enters a user name, recursively call this with each keypress; so if they are typing in Leroy, L is typed first and this code will retrieve all nodes where the username value starts with "L". Then the user types 'e' making it 'Le' etc etc.
func searchUsers(text: String) {
if text.count > 0 {
self.usersArray = [] //clear the array each time
let endingText = text + "\u{f8ff}"
databaseRef.child("profile").queryOrdered(byChild: "username")
.queryStarting(atValue: text)
.queryEnding(atValue: endingText)
.observeSingleEvent(of: .value, with: { snapshot in
for child in snapshot.children {
let childSnap = child as! DataSnapshot
let userObj = Mapper<UserModel>().map(JSONObject: childSnap.value!)
userObj?.uid = childSnap.key
if childSnap.key != self.loggedInUser?.uid { //ignore this user
self.usersArray.append(userObj!)
}
}
self.tableView.reloadData()
})
}
} //may need an else statement here to clear the array when there is no text
OP请求的代码,用于通过searchBar和if语句来处理搜索,以防止在没有文本的情况下进行搜索.这是调用上面的searchUsers函数的委托方法
OP requested code for handling the search through a searchBar and a if statement to prevent searching if there are is no text. Here's the delegate method for that which calls the searchUsers function above
func searchBar(_ searchBar: UISearchBar,
textDidChange searchText: String) {
self.searchUsers(text: searchText)
}
OP希望看到我的viewDidLoad函数原样令人兴奋,所以就在这里.
OP wanted to see my viewDidLoad function, as unexciting as it is, so here it is.
class ViewController: UIViewController, UISearchBarDelegate
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
这篇关于如何在我的searchcontroller中使用queryStartingAtValue搜索用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!