Reputation: 159
In my code i have nested query to fetch data and for that i have to go last level to fetch that value. for that i have to run for loop 4 time. instead of that is there any way in realm function to decrease this loop and access time?
var tax = 0.0
for item in items {
for menuItem in item.itemOrderMenu {
for customize in menuItem.menuSetItems {
for custom in customize.customizationItems where custom.isSelected {
tax += custom.taxAmount
}
}
}
}
is there a better approach to access nested data?
Upvotes: 0
Views: 541
Reputation: 271660
You could use a bunch of flatMap
, followed by a filter
and lastly a reduce
:
let customisationItems = items.lazy.flatMap { $0.itemOrderMenu.lazy.flatMap { $0.menuSetItems.lazy.flatMap { $0.customisationItems } } }
let tax = es.filter { $0.isSelected }.reduce(0.0, { $0 + $1.taxAmount })
I don't think you can reduce the nesting of that anymore.
Upvotes: 1