如何查询整个 Realm 数据库以获取特定数据,而不仅仅是来自特定字段或属性 [英] how to query the whole Realm database for a specific data and not only from a specific field or attribute

查看:51
本文介绍了如何查询整个 Realm 数据库以获取特定数据,而不仅仅是来自特定字段或属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含 5 个字段的表的领域数据库.字段是clientName"、occasions"、venue"、time"和date".在 uitableview 的显示中,我将能够根据 clientName 使用以下命令显示所有数据:

I have a realm database with a table with 5 fields. The fields are "clientName", "occasions" , "venue" , "time" and "date". In the display in uitableview I would be able to display all the data according to the clientName using :

 allEvent = realm.objects(AllEventClass.self).sorted(byKeyPath: "clientName", ascending: true)

现在我想设置一个 UISearchBar,它有一个函数来搜索整个数据库,而不考虑字段.无论用户从任何字段输入到搜索栏中的数据是什么,tableview 都会在 clientName 中显示与输入的搜索数据相关的结果.现在我只能使用 clientName 字段搜索数据库,结果当然是 clientName.例如,如果我只记得日期",但我不记得与该日期"关联的 clientName,那将是一个问题.我希望能够输入日期"或任何其他字段中的任何数据,并且仍然获得与其关联的 clientName 结果.目前这是我唯一可用的代码.

Now I want to set up a UISearchBar with a function to search the whole database irrespective of fields. No matter what data from any field the user entered into the searchbar, the tableview will show the result in clientName which is related to the searched data entered. For now I can search the database only using the clientName field and the result would of course be the clientName. For instance it would be a problem if I only remember the "date" but I could not remember the clientName associated with that "date". I want to be able to enter the "date" or any data from any other fields and still be given the clientName result associated with it. For now this is the only code available to me.

func searchBarSearchButtonClicked(_ searchBar: UISearchBar) {
    allEvent = AllEventClasses?.filter("clientName CONTAINS[cd] %@", searchBar.text!).sorted(byKeyPath: "clientName", ascending: true)


    tableView.reloadData()
}

非常感谢您.

推荐答案

这个问题可能是问几个问题之一.让我谈谈两个.为清楚起见,领域数据库将包含对象,而这些对象将具有属性(字段).因此,实际上并没有包含字段的表(因为这有点通用),因此将针对这些对象及其属性执行任何查询.

The question may be asking one of a couple of questions. Let me address two. For clarity, a Realm Database will contain objects and those objects will have properties (fields). So there isn't really a table with fields (as that's a bit generic) so any queries will be performed against those objects and their properties.

您可能会问如何同时查询数据库中的所有对象.

You may be asking how to query across all objects in your database at the same time.

Realm Result 是同构的集合类型,因此您不能将 Object 的不同子类存储在同一个 Results 对象中 - 这意味着您无法跨多个对象执行查询并将结果返回到一个 Result 对象中.一个简单的解决方案是查询每个对象类型.

A Realm Result is a homogenous collection type, so you can't store different subclasses of Object in the same Results object - that means you can't perform a query across multiple objects and return the results into one Result object. One simple solution is to query on each object type.

但是,我认为您的实际问题是询问如何跨对象内的多个属性进行查询.所以假设你的对象看起来像这样

However, I think your actual question is asking how to query across multiple properties within an object. So assuming your object looks like this

class AllEventClass: Object {
    @objc dynamic var clientName = ""
    @objc dynamic var occassions = ""
    @objc dynamic var venue = ""
    @objc dynamic var time = ""
    @objc dynamic var date = ""
}

假设您想查询 clientName、occasions 和 date 属性中的任何匹配项.

and suppose you want to query for any matches in the clientName, occassions and date properties.

let search = stringFromSearchBar
let eventResults = realm.objects(AllEventClass.self).filter("clientName contains[cd] %@ or date contains[cd] %@ or occassions contains[cd] %@", search, search, search)

如果您问其他问题,请告诉我,我会更新答案.

let me know if you asked something else and I will update the answer.

这篇关于如何查询整个 Realm 数据库以获取特定数据,而不仅仅是来自特定字段或属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆