首页 > 解决方案 > 如何使用 Firestore 搜索用户?

问题描述

我已经看到这为数据库提供了很多答案,但从未见过 Firestore。我在做这件事时遇到了一些困难。特别是,我似乎无法通过“用户名”对我的集合“用户”中的字段进行排序。我希望能够在我的表格视图中的单元格上显示用户的用户名。我尝试使用 NSDictionary 将用户的“用户名”附加到字典。您可能会说,我对编码很陌生。因此,任何答案都将不胜感激。这是我最近的尝试。我知道这是非常错误的,我只是不知道如何纠正它:

 var userArray = [NSDictionary]()
var filteredUsers = [NSDictionary?]()

let db = Firestore.firestore()

override func viewDidLoad() {
    super.viewDidLoad()

    searchController.searchResultsUpdater = self
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar


    searchController.searchResultsUpdater = self
    definesPresentationContext = true
    tableView.tableHeaderView = searchController.searchBar

    let userID = Auth.auth().currentUser?.uid
    db.collection("users").order(by: "username").getDocuments()
        { (snapshot, error) in
            self.userArray.append(snapshot.value as? Dictionary)

                self.findUsersTableView.insertRows(at: [IndexPath(row:self.userArray.count-1, section: 0)], with: UITableView.RowAnimation.automatic)
            if error != nil {
                print(error?.localizedDescription)
            }
    }

    }

标签: iosswiftgoogle-cloud-firestore

解决方案


推荐阅读