よろしくお願いします。
iOS 8.0
Swift 2.1
Realm 0.96.3
親子関係を持つ二つのクラスを定義し、status が true の Child を一つ以上持つ Parent を抽出したいと思っています。
class Child: Object {
dynamic var status = false
dynamic var name = ""
convenience init(name: String) {
self.init()
self.name = name
}
}
class Parent: Object {
let children = List<Child>()
dynamic var name = ""
convenience init(name: String) {
self.init()
self.name = name
}
}
class ViewController: UIViewController {
let realm = try! Realm()
override func viewDidLoad() {
super.viewDidLoad()
setSampleData()
procedure()
}
func procedure() {
let predicate = NSPredicate(format: "SUBQUERY(children, $c, $c.status == true).@count > 0")
print(realm.objects(Parent).filter(predicate).description)
}
func setSampleData() {
try! realm.write {
self.realm.deleteAll()
let parent1 = Parent(name: "Parent1")
let parent2 = Parent(name: "Parent2")
let parent3 = Parent(name: "Parent3")
let child1 = Child(name: "Child1")
let child2 = Child(name: "Child2")
let child3 = Child(name: "Child3")
let child4 = Child(name: "Child4")
let child5 = Child(name: "Child5")
self.realm.add(parent1)
self.realm.add(parent2)
self.realm.add(parent3)
self.realm.add(child1)
self.realm.add(child2)
self.realm.add(child3)
self.realm.add(child4)
self.realm.add(child5)
child1.status = false
child2.status = true
child3.status = false
child4.status = true
child5.status = false
parent1.children.append(child1)
parent1.children.append(child2)
parent2.children.append(child3)
parent2.children.append(child4)
parent3.children.append(child5)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
ビルドは成功しますが、以下のようなエラーが出ます。
Terminating app due to uncaught exception 'Invalid predicate expressions', reason: 'Predicate expressions must compare a keypath and another keypath or a constant value'
SUBQUERYについて理解ができていないと思いますが、
調べてもなかなかわかりません。
どなたかアドバイスを頂けますでしょうか。