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

Posted

技术标签:

【中文标题】iOS 如何在少数数据表中搜索数据 Parse swift 4【英文标题】:iOS How to search data at the few Data table Parse swift 4 【发布时间】:2018-09-08 11:13:07 【问题描述】:

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

我想从几个数据表库中进行搜索。

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

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 查询的所有子查询都应该在同一个类中。'

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

【问题讨论】:

一次搜索一个类 @Carpsen90 但我有关键字,我想在 2 个数据库中搜索 旁注:className: "fried" 也许你的意思是“朋友” 【参考方案1】:

一次搜索一个类,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()
            
        
    

【讨论】:

但如果我有 10 个基数,我需要 10 次编写这段代码?

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

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

从 Parse.com 检索搜索结果的数据

如何在 Objective-C iOs 8 上将 ParseObject 存储在本地数据存储和 Parse Cloud 中?

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

如何在 parse.com 上上传微调器数据

IOS 如何使用 Parse Swift 获取每个用户的个人资料图片