Parse-Swift 检查多列(复合/OR 语句)

发布于 2025-01-09 19:05:15 字数 1540 浏览 2 评论 0原文

我有一个表,数据库中有三列可以包含字符串,我需要检查所有三列,如果这些列中的任何都有我要查找的内容,则返回该行数据。

例如,我正在查找的字符串可能出现在一行的第一列(firstArtist)中,但可能出现在另一行的第二列(secondArtist)中,并且可能出现在第三列(thirdArtist)中。

我使用的是 Parse-Swift 而不是 Parse。

壁纸是我的模型类。

我尝试过这样的事情:

var testString = "example"

let query = Wallpaper.query()
            .where(Wallpaper.CodingKeys.firstArtist.rawValue == testString)
            .where(Wallpaper.CodingKeys.secondArtist.rawValue == testString)
            .where(Wallpaper.CodingKeys.thirdArtist.rawValue == testString)

但是没有从数据库中检索到任何内容,我认为这是因为它在第一个 where 语句中找不到任何内容,因此它不包含其余可能包含字符串的结果。

以下工作有效:

let query = Wallpaper.query()

       query.findAll { result in
            switch result {
            case let .success(wallpapers):
                var test: [Wallpaper] = []
                for item in wallpapers {
                    if item.artists.contains(testString) {
                        test.append(item)
                    }
                }
                completion(.success(test))
            case let .failure(error):
                completion(.failure(error))
            }
        }

但是循环遍历结果并手动过滤它们会导致我的应用程序花费超过十秒的时间来加载结果,因为我正在抓取所有不理想的内容。

如何在 Parse-Swift 中执行相当于 OR 语句/复合语句的操作?我已经浏览了文档,但找不到这方面的示例。

我找到了这个存储库 Github Parse-Swift Playground 其中包含 Parse-swift 开发者本身的示例,但什么也没有。

I have a table where I have three columns in my database that can contain a String, I need to check all three and return that row of data if ANY of those columns have what I'm looking for.

For example the string I'm looking for might appear in the first column (firstArtist) for one row but could appear in the second column (secondArtist) and possibly in the third column (thirdArtist) for another.

I'm using Parse-Swift not Parse.

Wallpaper is my model class.

I've tried something like this:

var testString = "example"

let query = Wallpaper.query()
            .where(Wallpaper.CodingKeys.firstArtist.rawValue == testString)
            .where(Wallpaper.CodingKeys.secondArtist.rawValue == testString)
            .where(Wallpaper.CodingKeys.thirdArtist.rawValue == testString)

But nothing gets retrieved from the database, I assume this is because since it doesn't find anything with the first where statement it doesn't include those results in the rest which could contain the string.

The following works:

let query = Wallpaper.query()

       query.findAll { result in
            switch result {
            case let .success(wallpapers):
                var test: [Wallpaper] = []
                for item in wallpapers {
                    if item.artists.contains(testString) {
                        test.append(item)
                    }
                }
                completion(.success(test))
            case let .failure(error):
                completion(.failure(error))
            }
        }

But the looping through the results and manually filtering them causes my app to take more that ten seconds to load the results, since I'm grabbing everything which isn't ideal.

How can I do the equivalent of a OR statement / Compound statement within Parse-Swift? I've looking through the documentation but I cant find and examples of this.

I found this repository Github Parse-Swift Playground Which contains examples from the Parse-swift Devs themselves but nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

蒲公英的约定 2025-01-16 19:05:15

您在查询中创建的语句本质上是一个 AND 语句,并且要求键位于所有 3 列中。 API 文档 显示有一个 or 查询约束可用。我建议查看 API 文档以查看所有可用的功能,因为 Playgrounds 并不打算实现 Parse-Swift 的所有功能。

您可以通过执行以下操作来实现您的目标:

let query1 = Wallpaper.query(Wallpaper.CodingKeys.firstArtist.rawValue == testString)

let query2 = Wallpaper.query(Wallpaper.CodingKeys.secondArtist.rawValue == testString)

let query3 = Wallpaper.query(Wallpaper.CodingKeys.thirdArtist.rawValue == testString)            

let subQueries = [query1, query2, query3]
let query = Wallpaper.query(or(queries: subQueries))

// Using async/await, but completion block will be similar to the code you provided
do {
  let wallpapers = try await query.findAll()
  print(wallpapers)
} catch {
  //Handle error
}

The statement you created in your query is essentially an AND statement and requires the key to be in all 3 columns. The API documentation shows that there's an or query constraint available. I recommend looking through the API documentation to see all of the features that are available, as the Playgrounds isn't intended to implement every feature of Parse-Swift.

You can accomplish your goal by doing:

let query1 = Wallpaper.query(Wallpaper.CodingKeys.firstArtist.rawValue == testString)

let query2 = Wallpaper.query(Wallpaper.CodingKeys.secondArtist.rawValue == testString)

let query3 = Wallpaper.query(Wallpaper.CodingKeys.thirdArtist.rawValue == testString)            

let subQueries = [query1, query2, query3]
let query = Wallpaper.query(or(queries: subQueries))

// Using async/await, but completion block will be similar to the code you provided
do {
  let wallpapers = try await query.findAll()
  print(wallpapers)
} catch {
  //Handle error
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文