过滤器正在返回具有匹配孩子的父母的所有孩子

Posted

技术标签:

【中文标题】过滤器正在返回具有匹配孩子的父母的所有孩子【英文标题】:Filter is returning all children for parent with matching child 【发布时间】:2017-05-25 21:29:36 【问题描述】:

我正在使用 Realm for Swift,我的结构如下:

class Navigation: Object 

    dynamic var key = 0
    dynamic var title: String?
    let companies = List<Companies>()

    override static func primaryKey() -> String? 
        return "key"
    


class Companies: Object 

    dynamic var key = 0
    dynamic var name: String?
    let locations = List<Locations>()

    override static func primaryKey() -> String? 
        return "key"
    


class Locations: Object 

    dynamic var key = 0
    ...
    dynamic var zip: String?
    let contacts = List<Contacts>()

    override static func primaryKey() -> String? 
        return "key"
    


class Contacts: Object 

    dynamic var key = 0
    dynamic var firstName: String?
    dynamic var lastName: String?
    ...

    override static func primaryKey() -> String? 
        return "key"
    

我正在尝试按邮政编码过滤位置,以便仅显示与给定邮政编码匹配的位置。我就是这样做的

companies = realm.objects(Navigation.self).filter("key = 4").first!.companies.filter(predicate).sorted(byKeyPath: "key")

key = 4 位是因为过滤器应该只在一个特定类别下的公司中搜索。

我遇到的问题是它会返回具有匹配位置的公司的所有位置。因此,如果我要查找的 zip 为 12345,并且 companyA 有一个匹配的位置,则返回 companyA 下的所有位置,即使它们不匹配。

如何将结果限制为匹配的位置?

【问题讨论】:

【参考方案1】:

如何将结果限制为匹配的位置?

现在您正在返回 Results&lt;Companies&gt;,但您似乎想要位置。我通过向CompaniesLocations 模型添加一些反向关系属性来完成我认为您正在寻找的东西,然后在领域中查询Locations 匹配zip == '12345' &amp;&amp; ANY parentCompanies.parentNavigation.key == 4

这是一个演示此功能的示例应用:

import UIKit
import RealmSwift

class Navigation: Object 

    dynamic var key = 0
    dynamic var title: String?
    let companies = List<Companies>()

    override static func primaryKey() -> String? 
        return "key"
    


class Companies: Object 

    dynamic var key = 0
    dynamic var name: String?
    let locations = List<Locations>()
    let parentNavigation = LinkingObjects(fromType: Navigation.self, property: "companies")

    override static func primaryKey() -> String? 
        return "key"
    


class Locations: Object 

    dynamic var key = 0
    // ...
    dynamic var zip: String?
    let contacts = List<Contacts>()
    let parentCompanies = LinkingObjects(fromType: Companies.self, property: "locations")

    override static func primaryKey() -> String? 
        return "key"
    


class Contacts: Object 

    dynamic var key = 0
    dynamic var firstName: String?
    dynamic var lastName: String?
    // ...

    override static func primaryKey() -> String? 
        return "key"
    


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate 

    var window: UIWindow?

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
        _ = try? FileManager.default.removeItem(at: Realm.Configuration.defaultConfiguration.fileURL!)
        let realm = try! Realm()

        // Objects Matching Query
        try! realm.write 
            // Locations
            let locations = Locations()
            locations.zip = "12345"

            // Companies
            let companies = Companies()
            companies.name = "Companies A"
            companies.locations.append(locations)

            // Nav
            let nav = Navigation()
            nav.key = 4
            nav.companies.append(companies)

            // Add to Realm
            realm.add(nav)
        

        let locationsIn12345AndNavigationKey4 = realm.objects(Locations.self)
            .filter("zip == '12345' && ANY parentCompanies.parentNavigation.key == 4")
        print(locationsIn12345AndNavigationKey4)

        return true
    

打印出来:

Results<Locations> (
  [0] Locations 
    key = 0;
    zip = 12345;
    contacts = RLMArray <0x6000000f2100> (

    );
  
)

【讨论】:

我可以使用它,但理想情况下,我希望它也拥有“拥有”这些位置的公司,以便用户知道他们正在查看的公司。有很多类似名称的位置,例如“总办公室”。 简单:locationsIn12345AndNavigationKey4.flatMap $0.parentCompanies 你能让这对代码更友好吗?这应该是进入过滤器的字符串是这些实际的函数调用吗?我认为这也必须通过反向关系来完成? 我不懂这些问题。你能改写一下吗? 您所做的评论,locationsIn12345AndNavigationKey4.flatMap $0.parentCompanies ,应该作为字符串进入filter() 函数,还是代码?如果是这样,我将其附加到什么?

以上是关于过滤器正在返回具有匹配孩子的父母的所有孩子的主要内容,如果未能解决你的问题,请参考以下文章

Django自递归外键过滤器查询所有孩子

NSPredicate 检索所有具有特定孩子的父母? (IOS)

jquery选择器需要选择父母的所有某些孩子

使用cheerio在没有孩子的父母中获取文本

如何在弹性搜索的过滤器聚合中引用多个嵌套级别?

Prisma:我怎样才能让所有孩子都处于某种状态?