iOS如何在少数Data表中搜索数据Parse swift 4

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS如何在少数Data表中搜索数据Parse swift 4相关的知识,希望对你有一定的参考价值。

我有搜索模块,它连接到解析。

我想从几个数据表基础上进行搜索。

为了搜索我使用下一个代码:

var searchResult = [String]()

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return searchResult.count
}


func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)

    cell.textLabel?.text = searchResult[indexPath.row]


    return cell
}

对于搜索栏这段代码:

 func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    searchBar.resignFirstResponder()

    let firstQuery = PFQuery(className: "fried")
    firstQuery.whereKey("best", contains: searchBar.text)

    let seccondQuery = PFQuery(className: "home")
    seccondQuery.whereKey("home", contains: searchBar.text)

    let query = PFQuery.orQuery(withSubqueries: [firstQuery,seccondQuery])
    query.findObjectsInBackground { (result, error) in
        if let objects = result {
            self.searchResult.removeAll(keepingCapacity: false)

            for object in objects {
                let question = object.object(forKey: "question") as! String
                let answer = object.object(forKey: "answer1") as! String
                let test = question + " " + answer

                self.searchResult.append(test)
            }

            DispatchQueue.main.async {
                self.tableView.reloadData()
                self.resignFirstResponder()
            }

        }
    }

}

我遇到了这种错误:

原因:'or查询的所有子查询应该在同一个类上。

可以在少数数据库中进行搜索吗?

答案

一次搜索一个类,orQuery在同一个类的子查询上完成

self.searchResult.removeAll(keepingCapacity: false) // Start by emptying the search resulys array

firstQuery.findObjectsInBackground { (result, error) in
    if let objects = result {
        for object in objects {
            let question = object.object(forKey: "question") as! String
            let answer = object.object(forKey: "answer1") as! String
            let test = question + " " + answer
            self.searchResult.append(test)
        }

        DispatchQueue.main.async {
            self.tableView.reloadData()
            self.resignFirstResponder()
        }
    }
}

seccondQuery.findObjectsInBackground { (result, error) in
    if let objects = result {
        for object in objects {
            let question = object.object(forKey: "question") as! String
            let answer = object.object(forKey: "answer1") as! String
            let test = question + " " + answer
            self.searchResult.append(test)
        }

        DispatchQueue.main.async {
            self.tableView.reloadData()
            self.resignFirstResponder()
        }
    }
}

或者更有说服力:

let queryArry = [firstQuery, secondQuery]
for query in queryArray {
    query.findObjectsInBackground { (result, error) in
        if let objects = result {
            for object in objects {
                let question = object.object(forKey: "question") as! String
                let answer = object.object(forKey: "answer1") as! String
                let test = question + " " + answer
                self.searchResult.append(test)
            }

            DispatchQueue.main.async {
                self.tableView.reloadData()
                self.resignFirstResponder()
            }
        }
    }

}

以上是关于iOS如何在少数Data表中搜索数据Parse swift 4的主要内容,如果未能解决你的问题,请参考以下文章

iOS 如何优化数据库 Parse swift 4 的查询

如何避免 Parse iOS 中的数据重复?

IOS - 如何在 Core Data Swift 中将数据插入到具有关系的不同表中

索引(Index)

在 Parse 数据库中查找最常出现的值

如何使用 Laravel 在 LIKE 搜索中使用 ORM 从两个表中获取数据