ios - 从 Realm 查询中排除对象 - 尝试按相关性排序(Swift)

标签 ios swift realm

我需要按相关性对搜索结果进行排序,同时尝试解决 Realm 的 NSPredicate 限制。

我当前的尝试重复结果:

if searchText.characters.count > 0 {
        //First search is attempting exact match
        relevantResults = Array(dataModel.terms.filter("%K BEGINSWITH[c] %@", "title", searchText).sorted(byProperty: "title"))
        //Appended Results are looking for 'close enough' but include results that were already in the first search
        relevantResults.append(contentsOf: Array(dataModel.terms.filter("%K CONTAINS[c] %@", "title", searchText).sorted(byProperty: "title")))
    } else {
        relevantResults = Array()
    }

我尝试使用其他一些谓词过滤器发挥创意,例如:

"title NOT BEGINSWITH[c] %@ AND title CONTAINS[c] %@"

"SELF NOT IN %@"

Realm 不支持,或者谓词过滤器无效。 无论哪种方式,我都需要找到一个解决方案来按相关性排序而不重复。

在单独的 TableView 部分中将显示更多搜索结果,我也想排除“最佳匹配”结果。

如果可能的话,我想避免循环从第一组结果中获取所有标题,但似乎我可能不得不诉诸于此,除非有一些我没有考虑的数组函数?有什么建议吗?

最佳答案

显然我的谓词字符串有点不对劲。

"title CONTAINS[c] %@ AND NOT title BEGINSWITH[c] %@"

最终为我工作。

关于ios - 从 Realm 查询中排除对象 - 尝试按相关性排序(Swift),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39837908/

相关文章:

ios - 在 UITableView 中实现 UISearchController 以过滤 User 对象数组

ios - 使用 Swift 将按钮添加到静态 TableView 标题

iOS 在dispatch_async 中更新RLMObject

xamarin - 获取异常 : "Already in a write transaction" in Realm for Xamarin iOS

ios - <从值中提取数据失败> 在 NSDictionary

ios - 使用照片框架请求照片时发生内存泄漏

ios - Material 组件选项卡栏 Controller 错误

ios - 在 Swift 的外部方法中获取当前 ViewController

ios - 应用程序上的深色模式开关

android - RealmRecylerAdapter 与 RxJava