Skip to content

Commit 47972a1

Browse files
committed
Updated getting ways and nodes from DB using predicate
- Instead of fetching all the records and applying filters, running RQL query before fetching the data will give better performance and less memory
1 parent 822ff4c commit 47972a1

File tree

2 files changed

+11
-33
lines changed

2 files changed

+11
-33
lines changed

GoInfoGame/GoInfoGame/AppQuestManager.swift

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,8 @@ class AppQuestManager {
2626
private let seedBox = BBox(minLat: 47.70312160869372, maxLat: 47.718964653825054, minLon: -122.20866792353317, maxLon: -122.18570621653987)
2727

2828
func getUpdatedQuest(elementId: String) -> DisplayUnitWithCoordinate? {
29-
let nodesFromStorage = dbInstance.getNodes().filter { n in
30-
n.tags.count != 0
31-
}
32-
let waysFromStorage = dbInstance.getWays().filter{w in w.tags.count != 0 }
29+
let nodesFromStorage = dbInstance.getNodes(NSPredicate(format: "tags.@count != 0"))
30+
let waysFromStorage = dbInstance.getWays(NSPredicate(format: "tags.@count != 0"))
3331
let theWay = waysFromStorage.first(where: {$0.id == Int64(elementId)})
3432
let theNode = nodesFromStorage.first(where: {$0.id == Int64(elementId)})
3533
let allQuests = QuestsRepository.shared.applicableQuests
@@ -68,33 +66,17 @@ class AppQuestManager {
6866

6967
// Fetches all the available quests from Database
7068
func fetchQuestsFromDB() -> [DisplayUnitWithCoordinate] {
71-
72-
let allOriginalNodes = dbInstance.getNodes().filter {$0.tags.count != 0 && ($0.point.latitude != 0.0 || $0.point.longitude != 0.0)}
7369

74-
75-
var nodesFromStorage: [StoredNode] = []
76-
for node in allOriginalNodes {
77-
autoreleasepool {
78-
if node.tags["ext:gig_complete"] != "yes" {
79-
nodesFromStorage.append(node)
80-
}
81-
}
82-
}
83-
84-
let allOriginalWays = dbInstance.getWays().filter {
85-
$0.tags.count != 0 && !$0.polyline.isEmpty
86-
}
87-
88-
var waysFromStorage: [StoredWay] = []
70+
let nodesFromStorage = dbInstance.getNodes(NSPredicate(format: """
71+
tags.@count != 0 AND
72+
tags['ext:gig_complete'] != 'yes'
73+
"""))
8974

90-
for way in allOriginalWays {
91-
autoreleasepool {
92-
if way.tags["ext:gig_complete"] != "yes" {
93-
waysFromStorage.append(way)
94-
}
95-
}
96-
}
97-
75+
let waysFromStorage = dbInstance.getWays(NSPredicate(format: """
76+
tags.@count != 0 AND
77+
polyline.@count > 0 AND
78+
tags['ext:gig_complete'] != 'yes'
79+
""" ))
9880

9981
let nodeElements = nodesFromStorage.map({ node in
10082
autoreleasepool {

GoInfoGame/GoInfoGame/DataBase/DatabaseConnector.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,6 @@ class DatabaseConnector {
184184
Fetches all the StoredNodes in the Database
185185
@returns a Results object containing StoredNodes
186186
*/
187-
func getNodes() -> Results<StoredNode> {
188-
return realm.objects(StoredNode.self)
189187

190188
func getNodes(_ predicate: NSPredicate) -> Results<StoredNode> {
191189
return realm.objects(StoredNode.self).filter(predicate)
@@ -194,8 +192,6 @@ class DatabaseConnector {
194192
Fetches all the storedWays in the Database
195193
@returns a Results object containing StoredWay
196194
*/
197-
func getWays() -> Results<StoredWay> {
198-
return realm.objects(StoredWay.self)
199195

200196
func getWays(_ predicate: NSPredicate) -> Results<StoredWay> {
201197
return realm.objects(StoredWay.self).filter(predicate)

0 commit comments

Comments
 (0)