我有三个对象嵌套在这样的列表中:
class Canteen: Object {
dynamic var name: String?
let lines = List<Line>()
}
class Line: Object {
dynamic var name: String?
let meals = List<Meal>()
}
class Meal: Object {
dynamic var name: String?
dynamic var vegan: Bool = false
}得到所有的食堂和所有的队伍和用餐是没有问题的。我现在做的是这样的:
let predicate = NSPredicate(format: "name == %@", selectedCanteenType.rawValue)
canteens = realm.objects(Canteen).filter(predicate)但是现在我只需要素食的食物。因此,我希望得到选定的食堂与所有的线,但只与饮食是素食。在realm中,是否可以过滤检索到的对象中的列表?
发布于 2016-05-13 07:32:30
Realm没有任何类型的深度过滤视图的概念,所以您不能有一个将相关对象中包含的List限制为纯素食的Results<Canteen>。
你可以做几件类似的事情。您可以添加反向关系属性,然后改为查询Meal对象:
class Canteen: Object {
dynamic var name: String?
let lines = List<Line>()
}
class Line: Object {
dynamic var name: String?
let meals = List<Meal>()
let canteens = LinkingObjects(fromType: Canteen.self, property: "lines")
}
class Meal: Object {
dynamic var name: String?
dynamic var vegan: Bool = false
let lines = LinkingObjects(fromType: Line.self, property: "meals")
}
let meals = realm.objects(Meal).filter("vegan = true AND ANY lines.canteens.name = %@", selectedCanteenType.rawValue)(或者更确切地说,一旦退出Realm 0.102.1,您就可以使用它了;目前它崩溃了)。
如果你只需要迭代每顿饭,但需要从食堂往下做,你可以这样做:
let canteens = realm.objects(Canteen).filter("name = %@ AND ANY lines.meals.vegan = true", selectedCanteenType.rawValue)
for canteen in canteens {
for line in canteen.lines.filter("ANY meals.vegan = true") {
for meal in line.meals.filter("vegan = true") {
// do something with your vegan meal
}
}
}不幸的是,这有一些重复,因为需要为每个引用级别重复过滤器。
发布于 2016-05-13 02:27:08
试试这个:
let predicate = NSPredicate(format: "name == %@", "")
var canteens: [Canteen] = realm.objects(Canteen).filter(predicate).map { can in
// Iterate through all the Canteens
let lines: [Line] = can.lines.map { (line: Line) in
// Iterate through all the lines in each canteene
// Filter all the Meals that are NOT vegan
let meals = line.meals.filter { $0.vegan == true }
line.meals = List<Meal>(meals)
return line
}
can.lines = List<Line>(lines)
return can
}发布于 2017-03-11 23:06:45
Realm允许它使用函数作为过滤的参数。这就是我目前使用的解决方案。
有两个过滤器函数:
func vegetarianFilter(_ meal: Meal) -> Bool {
if showVegetarianOnly {
if(meal.veg || meal.vegan){
return true
}
return false
}
return true
}
func filterEmptyLines(_ line: Line) -> Bool {
if(line.meals.filter(vegetarianFilter).count > 0){
return true
}
return false
}当用户选择了showVegetarianOnly = true时,这些功能会过滤所有不是素食或素食的餐点。此外,它还过滤所有行,然后没有剩余的饭(没有任何素食或素食)。
TableView最重要的功能:
override func numberOfSections(in tableView: UITableView) -> Int {
return canteenDay?.lines.filter(filterEmptyLines).count ?? 0
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return canteenDay?.lines.filter(filterEmptyLines)[section].meals.filter(vegetarianFilter).count ?? 0
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
let meal = canteenDay!.lines.filter(filterEmptyLines)[indexPath.section].meals.filter(vegetarianFilter)[indexPath.row]
cell.textLabel?.text = meal.meal
return cell
}https://stackoverflow.com/questions/37193959
复制相似问题