ios - 父类和子类的 Realm 过滤器

标签 ios swift realm

我想在父级和子级上实现过滤器,就好像搜索“chicken2”结果应该仅返回膳食为“chicken2”+膳食名称为“chicken2”的行,下面是我的带有查询和结果的模型类。

import Foundation
import RealmSwift


class Canteen: Object {
@objc dynamic var name: String?
let lines = List<Line>()

func initWithJSON(json: [String: Any]) {
    self.name = json["name"] as? String

    let lines = json["lines"] as! [[String: Any]]
    for lineJSON in lines {
        let line = Line()
        line.initWithJSON(json: lineJSON)
        self.lines.append(line)
    }

}

override static func primaryKey() -> String? {
    return "name"
}


} 


class Line: Object {
@objc dynamic var name: String?
var meals = List<Meal>()
let canteens = LinkingObjects(fromType: Canteen.self, property: "lines")

func initWithJSON(json: [String: Any]) {
    self.name = json["name"] as? String

    let meals = json["meals"] as! [[String: Any]]
    for mealJSON in meals {
        let meal = Meal()
        meal.initWithJSON(json: mealJSON)
        self.meals.append(meal)
    }
}

override static func primaryKey() -> String? {
    return "name"
}


}


class Meal: Object {
@objc dynamic var name: String?
@objc dynamic var vegan: Bool = false
let lines = LinkingObjects(fromType: Line.self, property: "meals")


func initWithJSON(json: [String: Any]) {
    self.name = json["name"] as? String
    self.vegan = json["isVegan"] as! Bool
}

override static func primaryKey() -> String? {
    return "name"
}


}

下面是我的 Controller 类的 viewDidLoad()

    override func viewDidLoad() {
    super.viewDidLoad()



    let file = Bundle.main.path(forResource: "mealss", ofType: ".json")
    let data = try! Data(contentsOf: URL(fileURLWithPath: file!))
    let json = try! JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)

    if let dict = json as? [String: Any] {
        let canteen = Canteen()
        canteen.initWithJSON(json: dict)
        try! realm.write {
            realm.add(canteen, update: true)
        }
    }


 realm.objects(Line.self).filter("ANY meals.name contains 'chicken2'")
    print(lines.description)        
}

下面是我的打印语句的输出。

enter image description here

下面是我使用的json文件。

{
"name": "canteen1",
"lines": [
    {
        "name": "line1",
        "meals": [
            {
                "name": "chicken2",
                "isVegan": false
            },
            {
                "name": "egges",
                "isVegan": false
            }
        ]
    },
    {
        "name": "line2",
        "meals": [
            {
                "name": "chicken",
                "isVegan": true
            },
            {
                "name": "egges",
                "isVegan": true
            }
        ]
    }
]
}

以下是我的预期输出。

[Line {
name = line1;
meals = List<Meal> <0x281301b90> (
    [0] Meal {
        name = chicken2;
        vegan = 0;
    }
);
}]

最佳答案

如果您像这样更改 Meal 类,则可以检索 Meal 对象并显示其父对象的名称。

class Meal: Object {
    @objc dynamic var name: String?
    @objc dynamic var vegan: Bool = false
    let lines = LinkingObjects(fromType: Line.self, property: "meals")
    var line: Line { return lines.first! } // <- added

    func initWithJSON(json: [String: Any]) {
        self.name = json["name"] as? String
        self.vegan = json["isVegan"] as! Bool
    }

    override static func primaryKey() -> String? {
        return "name"
    }

}

检索 Meal 对象并访问其父对象的名称,而不是 Line 对象。

let meals = realm.objects(Meal.self).filter("name contains 'chicken2'")
for meal in meals {
    print("name = \(meal.line.name!)")
    print(meal)
}

这是输出:

name = line1
Meal {
    name = chicken2;
    vegan = 0;
}

关于ios - 父类和子类的 Realm 过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53553169/

相关文章:

ios - 如何重置单例实例?

ios - 在将用户连接到服务器之前,如何等待 Socket.IO 建立套接字连接?

ios - 如何在 xcode 4.5 上从命令行运行 IOS 单元测试

ios - 如何制作自定义标题然后图像描述属于该标题并且在 TableView 中进行相同的过程

ios - Realm :更新属性缓慢

ios - 无法访问核心数据文件

ios - Apple Multiple Push Notifications 在通知托盘中不可见

ios - Firebase 数据库更新所有帖子的用户个人资料图片网址

ios - 无法从 Realm 文件中读取数据

ios - 从 UIWebView 内的 HTML 文件加载内部资源