使用嵌套子查询过滤领域 [英] Filtering Realm with nested subqueries

查看:116
本文介绍了使用嵌套子查询过滤领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序具有如下所示的数据.

My app has data that looks like this.

class ShelfCollection: Object {
  let shelves: List<Shelf>
}

class Shelf: Object {
  let items: List<Item>
}

class Item: Object {
  var name: String
  let infos: List<String>
}

我正在尝试将所有书架放在书架集合中,其中任何项目均按名称或信息列表中的元素与查询匹配.据我了解,该谓词应该是正确的,但是会崩溃.

I'm trying to get all shelves in a shelf collection where any items match the query either by name or by an element in their infos list. From my understanding this predicate should be correct, but it crashes.

let wildQuery = "*" + query + "*"
shelfResults = shelfCollection.shelves.filter(
    "SUBQUERY(items, $item, $item.name LIKE[c] %@ OR SUBQUERY($item.infos, $info, info LIKE[c] %@).@count > 0).@count > 0",
    wildQuery, wildQuery
)

它符合NSPredicate,但是在Realm试图解析它并抛出我时会崩溃

It complies as a NSPredicate, but crashes when Realm is attempting to parse it, throwing me

'RLMException', reason: 'Object type '(null)' not managed by the Realm'

我怀疑嵌套子查询可能会失败,但是我对NSPredicate的了解不足以确保.这是可以接受的查询吗,我该如何进行..工作?

I suspect the nested subquery might be what fails, but I don't know enough about NSPredicate to be sure. Is this an acceptable query, and how can I make it.. work?

推荐答案

这是一个答案和解决方案,但是对象的结构方式将存在许多问题,这可能会导致其他问题.由于许多对象都出现在其他对象中,因此很难创建匹配的数据集.

This is an answer and a solution but there's going to be a number of issues with the way the objects are structured which may cause other problems. It was difficult to create a matching dataset since many objects appear within other objects.

问题:

Realm当前无法在基元列表上进行过滤,因此该Item属性无法用于过滤:

Realm cannot currently filter on a List of primitives so this Item property will not work for filtering:

let infos: List<String>

但是,您可以创建另一个具有String属性的对象,并对该 object

However, you can create another object that has a String property and filter on that object

class InfoClass: Object {
    @objc dynamic var info_name = ""
}

然后Item类看起来像这样

and then the Item class looks like this

class Item: Object {
  var name: String
  let infos = List<InfoClass>()
}

,然后根据InfoClass object (不是字符串属性)进行过滤.所以你会有一些物体

and then you filter based on the InfoClass object, not it's string property. So you would have some objects

let info0 = InfoClass()
info0.info_name = "Info 0 name"

let info1 = InfoClass()
info1.info_name = "Info 1 name"

let info2 = InfoClass()
info2.info_name = "Info 2 name"

,它们存储在Item-> infos列表中.然后是问题

which are stored in the Item->infos list. Then the question

我正在尝试将所有书架都放在一个书架中...

I'm trying to get all shelves in a shelf collection...

状态要过滤的集合,在这种情况下为c0,表示其项目列表中包含特定信息的货架.可以说,我们想要得到那些在其列表中的项目中包含info2的货架

states you want to filter for a collection, c0 in this case, shelves whose items contain a particular info in their list. Lets say we want to get those shelves whose items have info2 in their list

 //first get the info2 object that we want to filter for
 guard let info2 = realm.objects(InfoClass.self).filter("info_name == 'Info 2 name'").first else {
    print("info2 not found")
    return
}

print("info2 found, continuing")

//get the c0 collection that we want to get the shelves for
if let c0 = realm.objects(ShelfCollection.self).filter("collection_name == 'c0'").first {
    let shelfResults = c0.shelves.filter("ANY items.infoList == %@", info2)
    for shelf in shelfResults {
        print(shelf.shelf_name)
    }
} else {
    print("c0 not found")
}

我省略了对name属性的过滤,因为您已经知道该怎么做.

I omitted filtering for the name property since you know how to do that already.

这里的问题是信息可能出现在许多物品中,而这些物品可能出现在许多货架清单中.因此,由于数据的深度以及我的测试数据,很难使过滤器返回不显眼的数据-如果我可以处理示例数据,对我来说可能更有意义.

The issue here is the infos could appear in many items, and those items could appear in many shelf lists. So because of the depth of data, with my test data, it was hard to have the filter return discreet data - it would probably make more sense (to me) if I had example data to work with.

无论哪种方式,答案都适用于该用例,但我认为另一种结构可能更好,但我不知道完整的用例很难这么建议.

Either way, the answer works for this use case, but I am thinking another structure may be better but I don't know the full use case so hard to suggest that.

这篇关于使用嵌套子查询过滤领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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