繁体   English   中英

搜索尚未获取的用户

[英]Search for users that haven't been fetch yet

我有一个简单的问题,我构建了一个代码块,用于获取数据库中的所有用户以进行搜索。 为将来成千上万的用户做准备,我以分页的方式编写了该方法,一次获取一定数量的用户。 这就是问题所在,当我搜索用户时,如果尚未通过滚动从数据库中检索到用户,我无法搜索那里的个人资料。 有人对我如何解决这个问题有建议吗?

这是我用来获取用户的代码:

//创建一个方法来获取一定数量的用户

    func fetchUsers() {

        if userCurrentKey == nil {
            USER_REF.queryLimited(toLast: 21).observeSingleEvent(of: .value) { (snapshot) in
                self.collectionView?.refreshControl?.endRefreshing()
                guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
                guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }

                allObjects.forEach({ (snapshot) in
                    let uid = snapshot.key
                    Database.fetchUser(with: uid, completion: { (user) in
                        self.users.append(user)
                        self.collectionView?.reloadData()
                    })
                })
                self.userCurrentKey = first.key
            }
        } else {
            USER_REF.queryOrderedByKey().queryEnding(atValue: userCurrentKey).queryLimited(toLast: 22).observeSingleEvent(of: .value, with: { (snapshot) in

                guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
                guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }

                allObjects.forEach({ (snapshot) in
                    let uid = snapshot.key
                    if uid != self.userCurrentKey {
                        Database.fetchUser(with: uid, completion: { (user) in
                            self.users.append(user)
                            self.collectionView?.reloadData()
                        })
                    }
                })
                self.userCurrentKey = first.key
            })
        }
    }
}

这是我用来对用户进行分页的代码: //一旦用户传递了一定数量的单元格,分页以获取下一组用户

override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
   if users.count > 20 {
        if indexPath.item == users.count - 1 {
            print("Fetching...")
            fetchUsers()
        }
    }
}

最后是我用来过滤用户的代码:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty{
            //if search text is empty fetch the users but display nothing
            inSearchMode = false
            filteredUsers = users
            self.collectionView?.refreshControl = refreshController
        } else {
            //if search text is not empty search for the users
            inSearchMode = true
            self.collectionView?.refreshControl = nil
            filteredUsers = self.users.filter { (user) -> Bool in
                return user.username.lowercased().contains(searchText.lowercased())
            }
        }
        //reload the table view data to update the displayed user
        self.collectionView?.reloadData()
    }

先感谢您!

我有一个简单的问题,我构建了一个代码块,用于获取数据库中的所有用户以进行搜索。 为将来成千上万的用户做准备,我以分页的方式编写了该方法,一次获取一定数量的用户。 这就是问题所在,当我搜索用户时,如果尚未通过滚动从数据库中检索到用户,我无法搜索那里的个人资料。 有人对我如何解决这个问题有建议吗?

这是我用来获取用户的代码:

//创建一个方法来获取一定数量的用户

    func fetchUsers() {

        if userCurrentKey == nil {
            USER_REF.queryLimited(toLast: 21).observeSingleEvent(of: .value) { (snapshot) in
                self.collectionView?.refreshControl?.endRefreshing()
                guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
                guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }

                allObjects.forEach({ (snapshot) in
                    let uid = snapshot.key
                    Database.fetchUser(with: uid, completion: { (user) in
                        self.users.append(user)
                        self.collectionView?.reloadData()
                    })
                })
                self.userCurrentKey = first.key
            }
        } else {
            USER_REF.queryOrderedByKey().queryEnding(atValue: userCurrentKey).queryLimited(toLast: 22).observeSingleEvent(of: .value, with: { (snapshot) in

                guard let first = snapshot.children.allObjects.first as? DataSnapshot else { return }
                guard let allObjects = snapshot.children.allObjects as? [DataSnapshot] else { return }

                allObjects.forEach({ (snapshot) in
                    let uid = snapshot.key
                    if uid != self.userCurrentKey {
                        Database.fetchUser(with: uid, completion: { (user) in
                            self.users.append(user)
                            self.collectionView?.reloadData()
                        })
                    }
                })
                self.userCurrentKey = first.key
            })
        }
    }
}

这是我用来对用户进行分页的代码: //一旦用户传递了一定数量的单元格,分页以获取下一组用户

override func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
   if users.count > 20 {
        if indexPath.item == users.count - 1 {
            print("Fetching...")
            fetchUsers()
        }
    }
}

最后是我用来过滤用户的代码:

func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        if searchText.isEmpty{
            //if search text is empty fetch the users but display nothing
            inSearchMode = false
            filteredUsers = users
            self.collectionView?.refreshControl = refreshController
        } else {
            //if search text is not empty search for the users
            inSearchMode = true
            self.collectionView?.refreshControl = nil
            filteredUsers = self.users.filter { (user) -> Bool in
                return user.username.lowercased().contains(searchText.lowercased())
            }
        }
        //reload the table view data to update the displayed user
        self.collectionView?.reloadData()
    }

先感谢您!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM